Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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)

What I have tried:

I tried to look into internet..
my code:
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");
}
}
}
Posted
Updated 27-Apr-17 12:57pm

1 solution

The problem is that your code assumes that when you receive a notification a file has shown up that you are the only one with access to it. Wrong! You get a notification that the file was created, NOT that it is done being written to. The process doing the writing has it locked for exclusive access and isn't sharing until it closes the file.

Your code has to wait. Either wait an arbitrary amount of time for the file the be closed and/or attempt to do your file operation and if it fails, wait a certain amount of time and retry the operation again and again until it works.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900