usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.IO;
usingSystem.Diagnostics;
usingSystem.Xml;
namespaceWindowsFormsApplication1
{
publicpartialclassFSWControl:Form
{
staticFileSystemWatcher watcher =newFileSystemWatcher();
publicFSWControl()
{
InitializeComponent();
stringStrPath=ReadrXML("watchdirectory","savedirectory");
WatcherStrat(StrPath,"*.*",true,true);
}
/// <summary>
/// 初始化監聽
/// </summary>
/// <param name="StrWarcherPath">須要監聽的目錄</param>
/// <param name="FilterType">須要監聽的文件類型(篩選器字符串)</param>
/// <param name="IsEnableRaising">是否啓用監聽</param>
/// <param name="IsInclude">是否監聽子目錄</param>
privatestaticvoidWatcherStrat(stringStrWarcherPath,stringFilterType,boolIsEnableRaising,boolIsInclude)
{
//初始化監聽
watcher.BeginInit();
//設置監聽文件類型
watcher.Filter=FilterType;
//設置是否監聽子目錄
watcher.IncludeSubdirectories=IsInclude;
//設置是否啓用監聽?
watcher.EnableRaisingEvents=IsEnableRaising;
//設置須要監聽的更改類型(如:文件或者文件夾的屬性,文件或者文件夾的建立時間;NotifyFilters枚舉的內容)
watcher.NotifyFilter=NotifyFilters.Attributes|NotifyFilters.CreationTime|NotifyFilters.DirectoryName|NotifyFilters.FileName|NotifyFilters.LastAccess|NotifyFilters.LastWrite|NotifyFilters.Security|NotifyFilters.Size;
//設置監聽的路徑
watcher.Path=StrWarcherPath;
//註冊建立文件或目錄時的監聽事件
watcher.Created+=newFileSystemEventHandler(watch_created);
//註冊當指定目錄的文件或者目錄發生改變的時候的監聽事件
watcher.Changed+=newFileSystemEventHandler(watch_changed);
//註冊當刪除目錄的文件或者目錄的時候的監聽事件
watcher.Deleted+=newFileSystemEventHandler(watch_deleted);
//當指定目錄的文件或者目錄發生重命名的時候的監聽事件
watcher.Renamed+=newRenamedEventHandler(watch_renamed);
//結束初始化
watcher.EndInit();
}
/// <summary>
/// 建立文件或者目錄時的監聽事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
privatestaticvoid watch_created(object sender,FileSystemEventArgs e)
{
//事件內容
}
/// <summary>
/// 當指定目錄的文件或者目錄發生改變的時候的監聽事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
privatestaticvoid watch_changed(object sender,FileSystemEventArgs e)
{
//事件內容
}
/// <summary>
/// 當刪除目錄的文件或者目錄的時候的監聽事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
privatestaticvoid watch_deleted(object sender,FileSystemEventArgs e)
{
//事件內容
}
/// <summary>
/// 當指定目錄的文件或者目錄發生重命名的時候的事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
privatestaticvoid watch_renamed(object sender,RenamedEventArgs e)
{
//事件內容
}
/// <summary>
/// 啓動或者中止監聽
/// </summary>
/// <param name="IsEnableRaising">True:啓用監聽,False:關閉監聽</param>
privatevoidWatchStartOrSopt(boolIsEnableRaising)
{
watcher.EnableRaisingEvents=IsEnableRaising;
}
/// <summary>
/// 讀取配置文檔
/// </summary>
/// <param name="StrNode">節點名字</param>
/// <param name="StrAttribute">屬性鍵</param>
/// <returns>屬性值</returns>
privatestringReadrXML(stringStrNode,stringStrAttribute)
{
string returnValue ="";
XmlDocument xmldoc =newXmlDocument();
stringStrPath=AppDomain.CurrentDomain.BaseDirectory;
xmldoc.Load(StrPath+"xml/Config.xml");
XmlNodeList nodeList = xmldoc.ChildNodes;
foreach(XmlNode item in nodeList)
{
if(item.Name.Trim().Equals("swfconvert"))
{
nodeList = item.ChildNodes;
foreach(XmlNode nodeitem in nodeList)
{
if(nodeitem.Name==StrNode)
{
returnValue = nodeitem[StrAttribute].Value;
return returnValue;
}
}
}
}
returnnull;
}
}
}node
轉自:http://blog.163.com/lilinrui_ruirui/blog/static/20115803620120510303739/ui