473,568 Members | 2,795 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(My String)
sw.close
else
dim sw as new streamwriter("C :\sample.txt")
sw.writeline(My String)
sw.close
end if
--
Huahe

Jul 21 '05 #1
3 1483
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***@discussi ons.microsoft.c om> 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(My String)
sw.close
else
dim sw as new streamwriter("C :\sample.txt")
sw.writeline(My String)
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***@discussi ons.microsoft.c om> 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(My String)
sw.close
else
dim sw as new streamwriter("C :\sample.txt")
sw.writeline(My String)
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***@discussi ons.microsoft.c om> 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
5139
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 documents (txt, MS Office files, pdf, pictures,…) from inside my app. It must start the file with the adequate external program (Notepad, MS Office...
3
2109
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 it not to close the window right away and I see the message: "This program cannot be run in DOS mode". I did some reading on it (every message I...
3
1655
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 immediately after the first, then an exception with the following message is thrown: "The process cannot access the file...
2
2093
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. Following is the example to reproduce it. I have a C++ project (mixed dll, managed and unmanaged), with classes: EncodeDecode.cpp, EncodeDecode.h,...
14
2566
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. Following is the example to reproduce it. I have a C++ project (mixed dll, managed and unmanaged), with classes: EncodeDecode.cpp, EncodeDecode.h,...
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 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...
3
4639
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 >> 0x804d008 _Delete unknown block >> registered : 0x804d138 >> 0x804d008 _Delete unknown block >> 0x804d098 _Delete ok
1
19873
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 it, it throws an exception. How to solave this problem? So second process can wait until first process completes its processing on the file. ...
3
2357
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 file and read it all the way through but I can not get it to continue looping after the file is finished. I am trying to get the new additions to...
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8117
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6275
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5498
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2101
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.