By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,526 Members | 2,207 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,526 IT Pros & Developers. It's quick & easy.

Problem with System.IO.File.Move and Catching Exceptions

P: n/a
I have a problem with this simple code below. Moving the files works
perfectly, but once in a blue moon the file that it is trying to move
dissapears before it can move it. and when this happens the
"System.IO.Move" goes in to a loop and keeps creating the file on the
destination folder with a 0byte size and keeps entering the error in
the log i have until someone stop the service. so what i cant figure
out is since it catches the Exception why does it keep creating the
file and keep trying to move that file that does not exist. Could i put
some code in the Exception that would automatically stop the service? i
need help !!!!!!!

Try

filePath = strPath & theFile.ToString & "_" & e.Name
System.IO.File.Move(e.FullPath, filePath)

Catch ex As Exception
sw = fso.AppendText("D:\WORKSTATIONSCANS\errlog.txt")
sw.WriteLine(Now() & " - MOVE - " & e.FullPath &
filePath & ex.ToString)
sw.Flush()
sw.Close()
End Try
error log shows:
7/25/2006 11:51:09 AM - MOVE -
d:\workstationscans\3845423.tif\\NASHVILLE-BROKERAGE\IMAGES$\28e367e0-d50e-468e-87ec-7a50f8c4ad1e_3845423.tifSystem.IO.FileNotFoundExce ption:
Could not find file "d:\workstationscans\3845423.tif".
File name: "d:\workstationscans\3845423.tif"
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.File.Move(String sourceFileName, String destFileName)
at WorkioScanWatcher.Service1.FileSystemWatcher1_Crea ted(Object
sender, FileSystemEventArgs e)
7/25/2006 11:51:17 AM - MOVE -
d:\workstationscans\3845423.tif\\NASHVILLE-BROKERAGE\IMAGES$\a9a947af-7993-4707-874e-e187e6e139c8_3845423.tifSystem.IO.FileNotFoundExce ption:
Could not find file "d:\workstationscans\3845423.tif".
File name: "d:\workstationscans\3845423.tif"
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.File.Move(String sourceFileName, String destFileName)
at WorkioScanWatcher.Service1.FileSystemWatcher1_Crea ted(Object
sender, FileSystemEventArgs e)
7/25/2006 12:12:14 PM - MOVE -
d:\workstationscans\3845423.tif\\NASHVILLE-BROKERAGE\IMAGES$\a0597cfa-9e42-43cc-ac5f-3de8e2544983_3845423.tifSystem.IO.FileNotFoundExce ption:
Could not find file "d:\workstationscans\3845423.tif".
File name: "d:\workstationscans\3845423.tif"
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.File.Move(String sourceFileName, String destFileName)

Jul 31 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I have two suggestions that should help and or that is just better coding:

1. Use System.IO.Path.Combine to combine your path strings together for
filePath.

2. Always use System.IO.File.Exists before any operation on a file.

If that does not work, then I would move the file manually, i.e. byte by
byte so you can detect easier if the file 'dissapeared'. Functions like .Move
are easy to use, but sometimes they come with a price.

======================================
David McCarter
www.vsdntips.com
VSDN Tips & Tricks .NET Coding Standards available at:
www.cafepress.com/vsdntips.20412485
"kk****@gmail.com" wrote:
I have a problem with this simple code below. Moving the files works
perfectly, but once in a blue moon the file that it is trying to move
dissapears before it can move it. and when this happens the
"System.IO.Move" goes in to a loop and keeps creating the file on the
destination folder with a 0byte size and keeps entering the error in
the log i have until someone stop the service. so what i cant figure
out is since it catches the Exception why does it keep creating the
file and keep trying to move that file that does not exist. Could i put
some code in the Exception that would automatically stop the service? i
need help !!!!!!!

Try

filePath = strPath & theFile.ToString & "_" & e.Name
System.IO.File.Move(e.FullPath, filePath)

Catch ex As Exception
sw = fso.AppendText("D:\WORKSTATIONSCANS\errlog.txt")
sw.WriteLine(Now() & " - MOVE - " & e.FullPath &
filePath & ex.ToString)
sw.Flush()
sw.Close()
End Try
error log shows:
7/25/2006 11:51:09 AM - MOVE -
d:\workstationscans\3845423.tif\\NASHVILLE-BROKERAGE\IMAGES$\28e367e0-d50e-468e-87ec-7a50f8c4ad1e_3845423.tifSystem.IO.FileNotFoundExce ption:
Could not find file "d:\workstationscans\3845423.tif".
File name: "d:\workstationscans\3845423.tif"
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.File.Move(String sourceFileName, String destFileName)
at WorkioScanWatcher.Service1.FileSystemWatcher1_Crea ted(Object
sender, FileSystemEventArgs e)
7/25/2006 11:51:17 AM - MOVE -
d:\workstationscans\3845423.tif\\NASHVILLE-BROKERAGE\IMAGES$\a9a947af-7993-4707-874e-e187e6e139c8_3845423.tifSystem.IO.FileNotFoundExce ption:
Could not find file "d:\workstationscans\3845423.tif".
File name: "d:\workstationscans\3845423.tif"
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.File.Move(String sourceFileName, String destFileName)
at WorkioScanWatcher.Service1.FileSystemWatcher1_Crea ted(Object
sender, FileSystemEventArgs e)
7/25/2006 12:12:14 PM - MOVE -
d:\workstationscans\3845423.tif\\NASHVILLE-BROKERAGE\IMAGES$\a0597cfa-9e42-43cc-ac5f-3de8e2544983_3845423.tifSystem.IO.FileNotFoundExce ption:
Could not find file "d:\workstationscans\3845423.tif".
File name: "d:\workstationscans\3845423.tif"
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.File.Move(String sourceFileName, String destFileName)

Aug 2 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.