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
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