C# 为什么FileSystemWatcher不';t火灾事件,但SMSTrace是否打开?
我创建了一个FileSystemWatcher对象来监视日志文件。它初始化为侦听所有可能的事件(lastwrite、lastaccess等),但在写入文件时不会触发事件 但是,如果我打开SMSTrace并用它收听该文件(并且清楚地看到该文件不断更新),filesystemwatcher就会触发事件。 SMSTrace对文件做了什么? 如何解释这一点?我如何修复它 代码如下:C# 为什么FileSystemWatcher不';t火灾事件,但SMSTrace是否打开?,c#,.net,C#,.net,我创建了一个FileSystemWatcher对象来监视日志文件。它初始化为侦听所有可能的事件(lastwrite、lastaccess等),但在写入文件时不会触发事件 但是,如果我打开SMSTrace并用它收听该文件(并且清楚地看到该文件不断更新),filesystemwatcher就会触发事件。 SMSTrace对文件做了什么? 如何解释这一点?我如何修复它 代码如下: private FileSystemWatcher fileWatcher; private FileSt
private FileSystemWatcher fileWatcher;
private FileStream fileStream;
private StreamReader streamReader;
private String fullPath = null;
private String dir = null;
private String fileName = null;
private void selectLogFile()
{
// TODO: try to restore previous settings before asking the user for the log file
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "C:\\";
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
fullPath = openFileDialog.FileName;
dir = Path.GetDirectoryName(fullPath);
fileName = Path.GetFileName(fullPath);
}
}
// TODO: what to do when file erases? (reboot) - clear the window?
public LogListener()
{
try
{
Thread selectFileThread = new Thread(new ThreadStart(selectLogFile));
selectFileThread.SetApartmentState(ApartmentState.STA);
selectFileThread.Start();
selectFileThread.Join();
// The user did not select a file - nothing to do
if (fullPath == null)
{
return;
}
// Create file listener to listen on changes to log
fileWatcher = new FileSystemWatcher(dir);
// Create a file stream to read the data from the log file
fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
// Create a stream reader from the fileStream to read text easily
streamReader = new StreamReader(fileStream);
// Watch for changes in LastWrite
fileWatcher.NotifyFilter = NotifyFilters.LastWrite;
// Only watch the log file.
fileWatcher.Filter = fileName;
// Add event handlers.
fileWatcher.Changed += new FileSystemEventHandler(OnChanged);
// Initial syncing of the file
readFile();
// Begin watching for events
fileWatcher.EnableRaisingEvents = true;
Log.Add("Started");
}
catch (Exception e)
{
Log.Add("Exception: " + e.Message);
}
}
void OnChanged(object source, FileSystemEventArgs e)
{
readFile();
}
public void readFile()
{
String line;
String bytesString = "";
Log.Add(DateTime.Now+":readFile()...");
// Some more code here...
}
这是一个长期的尝试,因为您没有提供任何代码,但是 您是否记得在FileSystemWatcher上将
EnableRaisingEvents
属性设置为true
?e、 g:
var w = new FileSystemWatcher("path");
w.Created += DoSomething;
w.Changed += DoSomething;
w.EnableRaisingEvents = true; // No events raised until you do this!
编辑:好的,你已经在做这个了。请忽略此答案,我不知道发生了什么:P
需要查找的是删除代码,然后在创建flesystemwatcher后重新添加您正在查看的文件夹。如果它这样做了,那么观察者将变得无效,并且您不会得到任何引发的事件。
今天我的代码中有一个bug,读了这篇文章后就知道了。你能编辑你的问题,把你的一些代码包括进去吗,可能还要比较一下SMSTrace是如何处理相同类型的问题的?SMSTrace唯一的特殊之处是它还使用了一个FileSystemWatcher。用于查看对SMS日志文件的更改。在NTFS卷上使用FileSystemWatcher不应该需要任何“特殊”的东西。像这样的环境问题往往与反恶意软件有关。