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

K

kkizer

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.FileNotFoundException:
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_Created(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.FileNotFoundException:
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_Created(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.FileNotFoundException:
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)
 
G

Guest

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
 

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