473,508 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

runtime error with io.file.delete

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
Nov 20 '05 #1
3 1800
* poindexter frink <de****@fleetcom.ca> scripsit:
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?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
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.
Nov 20 '05 #3
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.
Nov 20 '05 #4

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

Similar topics

0
5044
by: Jamey | last post by:
I perused old posts for an answer to this for at least an hour, and I've found a work-around, but no definitive answer. Synopsis of the problem: On NotInList or ctl.Requery commands where a...
6
2355
by: Bill Patel | last post by:
I am getting Runtime error on line 50. Please Help. Thank You Bill 1 <%@ Page Language="VB" %> 2 <%@ import Namespace="System.Data" %> 3 <%@ import Namespace="System.Data.SqlClient"...
1
1013
by: PaulieS | last post by:
Hi all. Am migrating a customer from IIS5 on W2K server to IIS6 on W2K3. Zipped all the websites and unzipped them to the identical locations on new server. Used IISMT to migrate metabase. ...
10
2130
by: MLH | last post by:
My A97 runtime installations are sometimes paused during the install process prompting user with messages saying the files are in use. Generally, I tell them to click IGNORE. Although I haven't...
1
1679
by: Pascalus | last post by:
Hi there! I have a problem with the delete operator to destroy a (char*). It used to work since today. I don't know what I may have changed in the project and/or the solution (probably in the...
0
3222
by: zfraile | last post by:
I'm getting this error from the JIT compiler at runtime, but only on my boss' machine, not my development machine. I believe the error is generated from a call to an Excel object, but I can't tell...
2
2734
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
3
2744
by: Baby Lion | last post by:
hi,everyone , one Runtime error occur when running , maybe it's about assigning memory . I need your help , thanks. It's about the travelling sellman problem . //#include "stdafx.h" ...
11
2806
by: LayneMitch via WebmasterKB.com | last post by:
Hello. This is a reference file from a book I read in which the core subject is the use of 'event listeners'. I'm trying to load the file in Firefox and it's giving me an error message: ...
0
7124
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7326
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,...
1
7046
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
7498
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
5053
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
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3195
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
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.