我的代码重复了3次:
private static void convert(object source, FileSystemEventArgs f)
{
string FileName;
FileName = f.FullPath;
string destinationFile = @"Y:\test\test.xml";
System.Threading.Thread.Sleep(2000);
try
{
Encoding utf8 = new UTF8Encoding(false);
Encoding ansi = Encoding.GetEncoding(1256);
System.Threading.Thread.Sleep(2000);
string xml = File.ReadAllText(FileName, ansi);
XDocument xmlDoc = XDocument.Parse(xml);
**Console.WriteLine("1st");**
File.WriteAllText(
destinationFile,
@"<?xml version=""1.0"" encoding=""utf-8""?>" + xmlDoc.ToString(),
utf8
);
}
以粗体检查以上内容。它写了3次。我刚刚把它测试一下。但为什么它会写出3次。这意味着正在写入的文件也写了3次。
我正在从filesystemwatcher函数调用此函数来监视文件夹(如果已更改),然后将文件转换为utf-8并将其放在目标文件中。
编辑1: 这是我的观察者。你能否检查一下是否正常:
private static void WatchFile()
{
watcher.Path = @"C:\project";
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.xml";
watcher.Changed += new FileSystemEventHandler(convert);
watcher.Error += new ErrorEventHandler(WatcherError);
Console.WriteLine("2nd");
watcher.EnableRaisingEvents = true;
}
仍然不知道为什么它会重复3次。
编辑2:
这是'我的完整代码:
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq;
class Test
{
class Class1
{
private static FileSystemWatcher watcher =
new FileSystemWatcher();
public static void Main()
{
WatchFile();
Console.ReadLine();
}
private static void WatchFile()
{
watcher.Path = @"C:\project";
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.xml";
watcher.Changed += new FileSystemEventHandler(convert);
watcher.Error += new ErrorEventHandler(WatcherError);
Console.WriteLine("2nd");
watcher.EnableRaisingEvents = true;
}
public static string CrL = "\r\n";
private static void convert(object source, FileSystemEventArgs f)
{
string FileName;
FileName = f.FullPath;
string destinationFile = @"Y:\test\OnAirNow.xml";
System.Threading.Thread.Sleep(2000);
try
{
Encoding utf8 = new UTF8Encoding(false);
Encoding ansi = Encoding.GetEncoding(1256);
System.Threading.Thread.Sleep(2000);
string xml = File.ReadAllText(FileName, ansi);
XDocument xmlDoc = XDocument.Parse(xml);
Console.WriteLine("1st");
File.WriteAllText(
destinationFile,
@"<?xml version=""1.0"" encoding=""utf-8""?>" + xmlDoc.ToString(),
utf8
);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
private static void WatcherError(object source, ErrorEventArgs e)
{
Exception watchException = e.GetException();
watcher = new FileSystemWatcher();
while (!watcher.EnableRaisingEvents)
{
try
{
WatchFile();
Console.WriteLine("I'm Back!!");
}
catch
{
System.Threading.Thread.Sleep(2000);
}
}
}
}
}
答案 0 :(得分:3)
使用FileSystemWatcher
的常见模式是在开始处理事件时将EnableRaisingEvents
设置为false
:
this.fileSystemWatcher = new FileSystemWatcher()
{
Path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
NotifyFilter = NotifyFilters.LastWrite,
Filter = Path.GetFileName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
};
this.fileSystemWatcher.Changed += this.ConfigChanged;
this.fileSystemWatcher.EnableRaisingEvents = true;
和
public void ConfigChanged(object sender, FileSystemEventArgs e)
{
try
{
this.fileSystemWatcher.EnableRaisingEvents = false;
s_logger.Info("Configuration file changed.");
// reload config here
s_logger.Info("Configuration settings reloaded.");
}
catch (Exception exception)
{
s_logger.Error(exception.Message);
s_logger.Error("Failed to reload configuration settings.");
}
finally
{
this.fileSystemWatcher.EnableRaisingEvents = true;
}
}
答案 1 :(得分:1)
FileSystemWatcher可能会为一个文件更改引发多个事件,请查看:
通用文件系统操作可能会引发多个事件。例如,当文件从一个目录移动到另一个目录时,可能会引发几个OnChanged和一些OnCreated和OnDeleted事件。移动文件是一项复杂的操作,由多个简单操作组成,因此可以引发多个事件。同样,某些应用程序(例如,防病毒软件)可能会导致FileSystemWatcher检测到其他文件系统事件。