runtime error with io.file.delete

P

poindexter frink

Fairly new to VB-style programming, so excuse me if I'm doing something
logical.

I'm working with CSV files, extracing certian columns, and ultimately
building a final CSV file.

I'm using the streamreader and streamwriter classes.

I'm having a runtime error that is inexplicable.

-- snip --
If Not infile Is Nothing Then
infile.Close()
End If

If IO.File.Exists(tempPath & "\old.csv") Then
IO.File.Delete(tempPath & "\old.csv")
End If
Rename(tempPath & "\new.csv", tempPath & "\old.csv")
-- snip --

What I'm doing here is testing to see if the infile (out.csv) is
initialized (if we're not in the first pass of the final CSV), and
closing it if it is. Then I'm deleting the old input file and replacing
it with the new input file.

The problem i'm having is that I get a runtime error at the
IO.File.Delete line, saying the "old.csv" is in use by another process.
I've put a breakpoint there, and infile is not initialized, so therefore
the file is not open by the streamreader class. Am I missing something
stupid like a flush? Or are my fundamentals off base.

-- dexter
 
H

Herfried K. Wagner [MVP]

* poindexter frink said:
I'm working with CSV files, extracing certian columns, and ultimately
building a final CSV file.

I'm using the streamreader and streamwriter classes.

I'm having a runtime error that is inexplicable.

-- snip --
If Not infile Is Nothing Then
infile.Close()
End If

If IO.File.Exists(tempPath & "\old.csv") Then
IO.File.Delete(tempPath & "\old.csv")
End If
Rename(tempPath & "\new.csv", tempPath & "\old.csv")
-- snip --

What I'm doing here is testing to see if the infile (out.csv) is
initialized (if we're not in the first pass of the final CSV), and
closing it if it is. Then I'm deleting the old input file and replacing
it with the new input file.

The problem i'm having is that I get a runtime error at the
IO.File.Delete line, saying the "old.csv" is in use by another process.
I've put a breakpoint there, and infile is not initialized, so therefore
the file is not open by the streamreader class. Am I missing something
stupid like a flush? Or are my fundamentals off base.

Can the file be deleted in the Windows Explorer?
 
P

poindexter frink

Can the file be deleted in the Windows Explorer?

Yes, but only after the program has terminated. I have looked over
everywhere that I open that file, and each time, I close it if it's open.

And as I said before, I break after I close the streamwriter, and debugging
the it reveals a null stream.

I am hesitant to go throwing try..catch statements all over the place
because they're costly, and promote bad programming practice. Although, it
seems that VB.NET has very few provisions for safe programming short of
using said try..catch statements.
 
C

Cor

Hi poindexter frink

I made a little example about this, I hope it fits your problem.

I did not do it, but making a "try catch and end try" block in this
situation, is of course very nice. You don't know before, if by instance
someone makes a file that has exactly the same name, as which you are using
as intermidiate outputfile (as example, but it can be everything).

It is bad using a try catch block, to prevent program errors that you know
that they exist and which does not come from the outside world of your
program.

I did make the example complete, because I did found it strange so I tested
it but with me it did works. Look also at the last rows, I think it is nicer
to use the io.move in combination with the io.delete instead of the rename
but only for the eyes not that it is better or something.

I had the idea that you did not close the input file, but to overcome a next
message if that was not the problem I made this.

And of course you don't have not to test if a file that you just have read
and written exist.

imports system.io
\\\
Dim inf As New StreamReader("c:\test.txt")
Dim outf As New StreamWriter("c:\nieuw.txt")
Dim intext As String
intext = inf.ReadLine
Do Until intext Is Nothing
outf.WriteLine(intext)
intext = inf.ReadLine
Loop
inf.Close()
outf.Close()
IO.File.Delete("c:\test.txt")
IO.File.Move("c:\nieuw.txt", "c:\test.txt")
'Rename("c:\nieuw.txt", "c:\test.txt")
///

I hope this helps a little bit?
Cor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top