Home All Groups Group Topic Archive Search About

FileIOException when trying to delete a file in a while loop

Author
24 May 2005 12:15 PM
Huahe
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

Author
24 May 2005 12:45 PM
Jan Hyde
"Huahe" <Hu***@discussions.microsoft.com>'s wild thoughts
were released on Tue, 24 May 2005 05:15:15 -0700 bearing the
following fruit:

Show quoteHide quote
>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

If that's dot net code then you need to be in a 'dotnet'
group.



Jan Hyde (VB MVP)

--
How many calfs did the mother cow have?
About heifer dozen.
(James D. Ertner)

[Abolish the TV Licence - http://www.tvlicensing.biz/]