I changed your stream to open
e.FullPath . I think you may also want to test that
e.FullPath is a file and not a directory.
public static void Run()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\text\";//args[1];
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "TransferComplete.*";
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
Console.WriteLine("q to exit");
while (Console.Read() != 'q')
{
}
watcher.EnableRaisingEvents = false;
watcher.Dispose();
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
System.Diagnostics.Debugger.Break();
try
{
using (StreamReader sr = new StreamReader(e.FullPath))
{
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
}
catch (Exception d)
{
Console.WriteLine("The file could not be read:");
Console.WriteLine(d.Message);
}
}
Reputation Points: 1749
Solved Threads: 735
Senior Poster
Offline 3,948 posts
since Feb 2009