473,503 Members | 3,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1468
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
5132
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
2105
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
1644
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
2081
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
2555
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
262
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
4631
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
19847
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
2347
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...
0
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7315
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6974
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7445
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4991
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3158
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3147
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.