I've seen this question before but with lots of answers and I don't know what's the best solution to solve this.
My scanner is running fine after a fresh start. It's scanning a directory for textfiles and the it has to do somthing with the file. The first file is always working but after the first file it becomes a gamble
Please help
Code
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\Data\IN";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.txt";
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
public void OnChanged(object source, FileSystemEventArgs e)
{
try
{
eventLog1.WriteEntry("Processing file : "+e.FullPath);
System.Collections.Generic.IEnumerable<String> lines = File.ReadAllLines(e.FullPath, System.Text.Encoding.Default);
System.Threading.Thread.Sleep(100);
if (File.Exists(Path.ChangeExtension(e.FullPath, ".old")))
File.Delete(Path.ChangeExtension(e.FullPath, ".old"));
File.Move(e.FullPath, Path.ChangeExtension(e.FullPath, ".old"));
}
catch (Exception ex)
{
eventLog1.WriteEntry("change : " + String.Concat(ex.Message, ex.StackTrace), EventLogEntryType.Error);
return;
}
}
The error is this :
cannot get access to file C:\Data\IN\kopie.txt because its beeing used by another proces.