我在4.0中使用FileSystemWatcher進行混淆。我覺得這非常有用,但我陷入了循環。我試圖監視每當一個ini被改變,並將其改回到正確的默認值(長篇故事),但改變事件複製到新文件導致它下降到一個循環...任何想法>?我玩弄了刪除和重新創建文件的想法,以避免觸發已更改的事件,但這導致了另一套我希望避免的程序問題。另外我想象我可以覆蓋文本,但這也帶來了同樣的問題。在此先感謝幫助FileSystemWatcher,取消訂閱事件
static void Main() { Watch (@"\\NoFault2010\Lexis\Data\Setup\", "tmconfig.ini", true); }
static void Watch (string path, string filter, bool includeSubDirs)
{
using (var watcher = new FileSystemWatcher (path, filter))
{
watcher.Changed += FileChanged;
watcher.EnableRaisingEvents = true;
Console.WriteLine("Do Not Close ... \n\nThis is a Temporary Configuration Manager for Time Matters ... \n\n\nI'm Listening ............");
Console.ReadLine();
}
}
static void FileChanged (object o, FileSystemEventArgs e)
{
string _right_stuff = @"\\NOFAULT2010\Lexis\Data\Templates\Programs\tmconfig.ini";
string _working = @"\\NOFAULT2010\Lexis\Data\Setup\tmconfig.ini";
System.Threading.Thread.Sleep(2000);
File.Copy(_right_stuff, _working, true);
Console.WriteLine("File {0} has been {1}", e.FullPath, e.ChangeType);
MAIL_IT("[email protected]", "TM Master.INI has been altered", "Check the Master INI and Yell At Ecopy Guy " + e.ChangeType + e.FullPath);
}
如何取消訂閱該事件以避免進入此循環。
-0.5:有可能是比賽在這裏ŧ帽子會不起作用。如果交付活動有延遲(例如,他們排隊),然後更改事件將在FileChanged()退出後發佈(並且inEdit已返回false)。 – payne 2011-02-08 00:47:45