Nepethya Rana

Nepethya Rana

  • NA
  • 335
  • 151.9k

IO Exception. File Watcher Program

Apr 27 2017 5:19 PM
Hi, what i am doing is to create a dir watcher which will watch the dir for example "A" and copy the file of certain name to another folder once it sees the file in the dir A to destination dir B. I dropped the file to the Dir A and it worked. but when i ran the program in my local machine and set dir to be watched and destination dir in server, it throws me the following error.
How do i handle it?
Unhandled Exception: System.IO.IOException: The process cannot access the file '\\IP-exapp-101\c$\WorkDir\WatchedDir\_f$12345.dat' because it is being used by another process.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
at System.IO.File.Copy(String sourceFileName, String destFileName)
at iSpy_File.Program.OnChanged(Object source, FileSystemEventArgs e) in c:\users\xuser\documents\visual studio 2015\Projects\iSpy_File\iSpy_File\Program.cs:line 68
at System.IO.FileSystemWatcher.OnCreated(FileSystemEventArgs e)
at System.IO.FileSystemWatcher.NotifyFileSystemEventArgs(Int32 action, String name)
at System.IO.FileSystemWatcher.CompletionStatusChanged(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* overlappedPointer)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
----------------------------------------------------------------------
my code:
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
// If a directory is not specified, exit program.
if (args.Length != 3)
{
// Display the proper way to call the program.
Console.WriteLine("Please Provide Source and destination directory: iSpy_Folder.exe <Source dir> <Destination dir>");
return;
}
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.*";
//// will track changes in sub-folders as well
watcher.IncludeSubdirectories = true;
// Add event handlers.
// watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
// watcher.Deleted += new FileSystemEventHandler(OnChanged);
// watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit Program.");
while (Console.Read() != 'q') ;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
string[] args = System.Environment.GetCommandLineArgs();
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
if ("_F$"== e.FullPath.Substring(e.FullPath.LastIndexOf("\\")+1, 3).ToUpper())
{
if (File.Exists(e.FullPath))
{
File.Copy(e.FullPath, args[2] + "\\" + e.FullPath.Substring(e.FullPath.LastIndexOf("\\")));
Console.WriteLine("File Copied");
}
}
}
 

Answers (1)