我正在使用c#windows窗体处理文件监视器应用程序。要求是,我将有多个文件夹,当在这些文件夹中创建文件时,我需要跟踪它们并根据更改的文件运行集成包。以下是我的代码
private void btnStart_Click(object sender, EventArgs e) //start
{
Watching.Items.Clear();
foreach (string sMonitorFolder in Global.DirectoryToWatch())
{
if (!(Directory.Exists(sMonitorFolder)))
{
MessageBox.Show("Directory does not exist", "Exit", MessageBoxButtons.OK);
return;
}
_watcher.Path = sMonitorFolder;
_watcher.Filter = comboBox1.Text;
Watching.Items.Add("Watching" + sMonitorFolder +" ...");
_watcher.IncludeSubdirectories = true;
_watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.CreationTime | NotifyFilters.FileName |
NotifyFilters.DirectoryName | NotifyFilters.LastWrite | NotifyFilters.Size;
_watcher.Created += new FileSystemEventHandler(OnChanged);
_watcher.EnableRaisingEvents = true;
}
btnStart.Enabled = false;
btnStop.Enabled = true;
textBox1.Enabled = false;
comboBox1.Enabled = false;
btnWrite.Enabled = true;
}
onChanged事件处理程序
private void OnChanged(object sender, FileSystemEventArgs e)
{
// Add event details in listbox.
this.Invoke((MethodInvoker) (() =>
Watching.Items.Add($"{e.FullPath}|{e.ChangeType}|{e.Name}|{DateTime.Now}")));
}
我正在存储需要在xml文件中观看的所有文件夹。我有一个全局的cs,我正在阅读这个xml文件并获取我需要观察的各种目录。
xml文件的结构
<?xml version="1.0" encoding="utf-8" ?>
<root>
<FolderToWatch>
<value>D:/FolderService/Test</value>
<value>D:/FolderService/Data</value>
</FolderToWatch>
<FileExtension>
<value>.txt</value>
</FileExtension>
<LogFilePath>
<value>D:/FileWatcherLog/</value>
</LogFilePath>
</root>
以下是我的global.cs
static class Global
{
static string output;
static List<string> filter;
static XmlDocument doc;
/// <summary>
///
/// </summary>
static Global()
{
doc = new XmlDocument();
doc.Load(Path.Combine(Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName, "setting.xml"));
//doc.Load(Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location, "setting.xml"));
output = doc.SelectSingleNode("/root/LogFilePath/value").InnerText;
filter = new List<string>();
XmlNodeList exNodeList = doc.SelectNodes("/root/FileExtension/value");
foreach (XmlNode nd in exNodeList)
filter.Add(nd.InnerText);
}
/// <summary>
/// Log : methods for performing the log write
/// </summary>
/// <param name="Message"></param>
public static void WriteLog(string Message)
{
DateTime dateTime = System.DateTime.UtcNow.AddMinutes(330);
if (!Directory.Exists(output))
Directory.CreateDirectory(output);
string logfileName = dateTime.ToString("dd_MMM_yy") + "_" + ".txt";
StreamWriter streamwriter = new StreamWriter(output + logfileName, true);
streamwriter.WriteLine(DateTime.Now + ": " + Message);
streamwriter.Close();
streamwriter.Dispose();
}
/// <summary>
/// DirectoryArray : List of all the directory that required to be watched.
/// </summary>
/// <returns>Array</returns>
public static Boolean GetFileExtention(string filePath)
{
string fileEX = Path.GetExtension(filePath);
return filter.Contains(fileEX);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public static List<string> DirectoryToWatch()
{
List<string> liFolderToWatch = new List<string>();
XmlNodeList ndWatchFolders = doc.SelectNodes("/root/FolderToWatch/value");
foreach (XmlNode nd in ndWatchFolders)
liFolderToWatch.Add(nd.InnerText);
return liFolderToWatch;
}
}
代码仅适用于一个目录,而不适用于所有目录。它适用于在xml文件中指定的最后一个目录,并且先前的目录将被忽略。我需要为我在xml文件中的foldertowatch节点中指定的每个目录创建多个观察者。任何人都可以帮我解决这个问题吗?