Why do i get an IOException (The process cannot access the file because it is being used by another process.), when i copy the same file 3 Times into Folder A?
class FileWatcher {
FileSystemWatcher fsw;
FileInfo file;
string destination = @"C:\FileMover\B\";
Random random;
public FileWatcher() {
fsw = new FileSystemWatcher();
random = new Random();
fsw.Path = @"C:\FileMover\A";
fsw.Created += fsw_Created;
fsw.EnableRaisingEvents = true;
}
void fsw_Created(object sender, FileSystemEventArgs e) {
string destinationFileName = destination + e.Name;
if (!File.Exists(destinationFileName)) {
file = new FileInfo(e.FullPath);
file.MoveTo(destinationFileName);
}
else {
file = new FileInfo(e.FullPath);
file.MoveTo(destinationFileName + random.Next());
}
file = null;
}
}
Main:
class Program {
static void Main(string[] args) {
FileWatcher watcher = new FileWatcher();
while (Console.ReadKey().Key != ConsoleKey.Q) {
}
}
}
After 2 times folder B contains 2 files (sourceFileName and sourceFileName1274968236)
When i debug the section no Exception will be thrown.