473,378 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

FileIOExcepion when trying to delete a file for the second time

I try to delete a file in a for each loop. My code checks if the file exists
and if it does, it will delete the file and create a new file with the same
name.
The first time it works perfect, but the second time it gives me a
FileIOException. I want to prevent this from happening. What can i do to make
sure the file isn't in use anymore the second time i try to delete it?

for each dr in table.rows
if file.exist("C:\sample.txt") then
file.delete("C:\sample.txt")
dim sw as new streamwriter("C:\sample.txt")
sw.writeline(MyString)
sw.close
else
dim sw as new streamwriter("C:\sample.txt")
sw.writeline(MyString)
sw.close
end if
--
Huahe

Jul 21 '05 #1
3 1463
Hi Huahe,

As you seem to be deleting the file you created a split second earlier I suspect the file isn't fully created yet, only buffered for creation. This isn't normally an issue since it should be created soon enough, but when you do tight loops like that you may bump into this. This is also a reason why for instance MS Sql Server uses unbuffered IO operations since it needs to know for sure that the data has been changed on the harddrive, and not just cached for change at a later time.
On Tue, 24 May 2005 16:36:04 +0200, Huahe <Hu***@discussions.microsoft.com> wrote:
I try to delete a file in a for each loop. My code checks if the file exists
and if it does, it will delete the file and create a new file with the same
name.
The first time it works perfect, but the second time it gives me a
FileIOException. I want to prevent this from happening. What can i do to make
sure the file isn't in use anymore the second time i try to delete it?

for each dr in table.rows
if file.exist("C:\sample.txt") then
file.delete("C:\sample.txt")
dim sw as new streamwriter("C:\sample.txt")
sw.writeline(MyString)
sw.close
else
dim sw as new streamwriter("C:\sample.txt")
sw.writeline(MyString)
sw.close
end if


--
Happy coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #2
Thank you for your answer Morten. I understand now why it doesn't work. But
do you know a solution for my problem?
Can i maybe pause the loop for a moment? Or make sure the file is created
before i go further with my program execution.

--
Huahe
"Morten Wennevik" wrote:
Hi Huahe,

As you seem to be deleting the file you created a split second earlier I suspect the file isn't fully created yet, only buffered for creation. This isn't normally an issue since it should be created soon enough, but when you do tight loops like that you may bump into this. This is also a reason why for instance MS Sql Server uses unbuffered IO operations since it needs to know for sure that the data has been changed on the harddrive, and not just cached for change at a later time.
On Tue, 24 May 2005 16:36:04 +0200, Huahe <Hu***@discussions.microsoft.com> wrote:
I try to delete a file in a for each loop. My code checks if the file exists
and if it does, it will delete the file and create a new file with the same
name.
The first time it works perfect, but the second time it gives me a
FileIOException. I want to prevent this from happening. What can i do to make
sure the file isn't in use anymore the second time i try to delete it?

for each dr in table.rows
if file.exist("C:\sample.txt") then
file.delete("C:\sample.txt")
dim sw as new streamwriter("C:\sample.txt")
sw.writeline(MyString)
sw.close
else
dim sw as new streamwriter("C:\sample.txt")
sw.writeline(MyString)
sw.close
end if


--
Happy coding!
Morten Wennevik [C# MVP]

Jul 21 '05 #3
The solution depends on what you are trying to do. It may be that you don't have to use a file at all.

Try creating a new thread where you explain your goals, what you do now, and why it doesn't work.

On Tue, 31 May 2005 13:32:02 +0200, Huahe <Hu***@discussions.microsoft.com> wrote:
Thank you for your answer Morten. I understand now why it doesn't work. But
do you know a solution for my problem?
Can i maybe pause the loop for a moment? Or make sure the file is created
before i go further with my program execution.


--
Happy coding!
Morten Wennevik [C# MVP]
Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different...
3
by: John | last post by:
Greetings, I am running XP Professional. I have an exe file written in VC++. When I try to run it it flashes a command window at me and shuts down. I created a shortcut for it so I could set...
3
by: PB | last post by:
As part of an ASP.NET WEb Application I have a routine (relevant portion is below) that lets users rotate a photo (jpg or gif). The routine works just fine if it is run once. If run a second time...
2
by: AAguiar | last post by:
Thanks for your replies. Last week, I continued working with this problem. Trying to reproduce the error, I developed a short example and found that the problem is related to strong name dll. ...
14
by: AAguiar | last post by:
Thanks for your replies. Last week, I continued working with this problem. Trying to reproduce the error, I developed a short example and found that the problem is related to strong name dll. ...
4
by: Huahe | last post by:
I try to delete a file in a for each loop. My code checks if the file exists and if it does, it will delete the file and create a new file with the same name. The first time it works perfect, but...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
1
by: ABCL | last post by:
Hi All, I am working on the situation where 2 different Process/Application(.net) tries to open file at the same time....Or one process is updating the file and another process tries to access...
3
by: Paulers | last post by:
Hello all, I have a log file that is appended to in real time by an application. I would like to write a loop in vb.net that reads new entires and parses them. I can get my application to open a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.