Strange Error behavior from Rename function in vb.net 2003

C

Carsten at home

Im writing a small utility that renames a file from one format to another.
Trying to be a good little programmer I coded the actual rename function as
follows.
Private Sub btnChange_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnChange.Click

Dim x As Integer

For x = 0 To lstFiles.Items.Count - 1

' Rename each file.

MessageBox.Show("renaming " + FileSpec + lstFiles.Items(x) + " to " + _

FileSpec + LstRenamedFiles.Items(x))

Try

Rename(FileSpec + lstFiles.Items(x), FileSpec +
LstRenamedFiles.Items(x))

Catch exp As ArgumentException

MessageBox.Show("Invalid path name - " + exp.Message)

Catch Exp As FileNotFoundException

MessageBox.Show("File to be renamed does not Exsist - " +
exp.Message)

Catch exp As IOException

MessageBox.Show("Renamed File already Exsists - Skipping - " +
exp.Message)

Catch exp As Exception

MessageBox.Show("Non-Specific Fatal error occurred - " +
exp.Message)

End Try

Next


End Sub

In my unit testing however I found some rather odd behavior. Speficically
when the file to be renamed, already exisists a FileNotFoundException is
being thrown, and NOT the IOException that I was expecting. Heres the
relevant piece out of the documentation for the Rename function.

Exceptions/Errors
Exception type Error number Condition
ArgumentException 5 Pathname is invalid .
FileNotFoundException 53 OldPath file does not exist.
IOException 58 NewPath file already exists.
IOException 75 Access is invalid.
IOException 74 Cannot rename to different device.


Is this known, and expected behavior, and the documentation is incorrect, or
have I gone totally loopy here ?

Any insight that can be provided is greatly appreciated!!

VC
 
H

Herfried K. Wagner [MVP]

Carsten at home said:
In my unit testing however I found some rather odd behavior. Speficically
when the file to be renamed, already exisists a FileNotFoundException is
being thrown, and NOT the IOException that I was expecting. Heres the
relevant piece out of the documentation for the Rename function.

Exceptions/Errors
Exception type Error number Condition
ArgumentException 5 Pathname is invalid .
FileNotFoundException 53 OldPath file does not exist.
IOException 58 NewPath file already exists.
IOException 75 Access is invalid.
IOException 74 Cannot rename to different device.


Is this known, and expected behavior, and the documentation is incorrect, or
have I gone totally loopy here ?

I created the file "C:\a.txt" and "C:\bla.txt" and used this Code:

\\\
Rename("C:\a.txt", "C:\bla.txt")
///

This throws a 'System.ArgumentException' on my Windows XP Professional
machine running .NET 1.0.
 
C

Carsten at home

Herfried K. Wagner said:
I created the file "C:\a.txt" and "C:\bla.txt" and used this Code:

\\\
Rename("C:\a.txt", "C:\bla.txt")
///

This throws a 'System.ArgumentException' on my Windows XP Professional
machine running .NET 1.0.


Thank you for the response. I tried a similar single purpose test project
and got the same results that you do when I do that. Also for what its worth
Im using XP Pro, with 1.1 of .Net.
 
J

Jim Cantwell [MSFT]

I have confirmed this is a bug in 7.0 and 7.1 versions of the Visual Basic .NET runtime.

A simple workaround is to use System.IO.File.Move( fromFile, toFile ) instead of Rename.
 
C

Carsten at home

Jim Cantwell said:
I have confirmed this is a bug in 7.0 and 7.1 versions of the Visual Basic ..NET runtime.

A simple workaround is to use System.IO.File.Move( fromFile, toFile ) instead of Rename.
AHA!!! thank you much for the information! I will try the new function so
that I can get more consistent error responses!

Thanks again for the response!!

VC
 

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