0

Made a C# console application where I use a FileWathcerSystem to track when new files are added to a certain folder. I have added an outside DLL as reference (which I believe is AMD64 since I get error when I use x86 instead of x64).

Now, everything works so far, I can use the methods from the DLL. But, when I try to read the text from the created .txt I get the following error:

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll

Now, when I look at the references that are added by default:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.6.1\System.Data.DataSetExtensions.dll

Does this mean it's using the x86 dll as standard?

FileSystemWatcher fwatcher = new FileSystemWatcher();
fwatcher.Path = "C:\\FileWatcher";
fwatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName;
fwatcher.Created += new FileSystemEventHandler(FileCreated);
fwatcher.EnableRaisingEvents = true;

This is the method that I try to run that is called when a new file is created )

private static void FileCreated(object sender, FileSystemEventArgs e)
{
    Console.WriteLine("File created");
    System.Threading.Thread.Sleep(1000);
    String text = File.ReadAllText(@e.FullPath);
    StringReader reader = new StringReader(text);
    Console.WriteLine(e.FullPath.ToString());
    Console.WriteLine(text);
}

Also, all of a sudden I get double events. I added sleep timer since I got an error that the file doesn't exist.

Domysee
  • 12,718
  • 10
  • 53
  • 84
Mårten Cederholm
  • 87
  • 1
  • 3
  • 10
  • You mentioned outside DLL as reference. Name of DLL ? – Akshay May 31 '16 at 11:06
  • 1
    remove `@`, then move `console.writeline(e.FullPath)` before `stringreader`. `fullpath` is a string already you don't need to convert it to string – Claudius May 31 '16 at 11:07
  • @Claudius Thank you. It works now. But like I said, all of a sudden I get double events :( – Mårten Cederholm May 31 '16 at 11:13
  • not sure.. but already a question for the same http://stackoverflow.com/questions/1764809/filesystemwatcher-changed-event-is-raised-twice – Akshay May 31 '16 at 11:16
  • try adding `WatcherChangeTypes wct = e.ChangeType; Console.WriteLine("File {0} {1}", e.FullPath, wct.ToString());` to see if only created event was called – Claudius May 31 '16 at 11:22
  • fwatcher.EnableRaisingEvents = false; <<< added this to the start of the method fwatcher.EnableRaisingEvents = true; <<< added at the end of the method Any risk? – Mårten Cederholm May 31 '16 at 11:34
  • An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: The path is not of a legal form. It gives me this for fwatcher.EnableRaisingEvents = true; – Mårten Cederholm May 31 '16 at 11:55

0 Answers0