我正在尝试编写一个简单的C#服务来查看文件夹并在更新该文件夹时通知我的应用程序。我找到了一些使用FileSystemWatcher的很好的例子,但当我将路径设置为c:\ Program Files(x86)下的文件夹时,它根本不起作用。
当我设置其他文件夹的路径时,我的代码工作正常。我想也许因为名字中的括号而未能阅读路径。我尝试在C盘的根目录中命名一个文件夹并使用括号,但这也不起作用。我不确定这里究竟是什么问题或如何解决它。这是权限问题吗?有没有办法逃避路径中的字符,即使它们在技术上是“合法的”?
private void InitializeComponent()
{
this.watchReflectAssetFolder = new System.IO.FileSystemWatcher();
((System.ComponentModel.ISupportInitialize)(this.watchReflectAssetFolder)).BeginInit();
//
// watchReflectAssetFolder
//
this.watchReflectAssetFolder.EnableRaisingEvents = true;
this.watchReflectAssetFolder.IncludeSubdirectories = true;
this.watchReflectAssetFolder.Path = "C:\\Program Files (x86)\\Reflect Systems\\ReflectDistributor\\Cache\\WEBCASTS";
this.watchReflectAssetFolder.Changed += new System.IO.FileSystemEventHandler(this.watchReflectAssetFolder_Changed);
//
// LevisAssetCopyService
//
this.ServiceName = "LevisAssetCopyService";
((System.ComponentModel.ISupportInitialize)(this.watchReflectAssetFolder)).EndInit();
}
private void watchReflectAssetFolder_Changed(object sender, System.IO.FileSystemEventArgs e)
{
string ChangeType = e.ChangeType.ToString();
//write a log entry for the appropriate changetype.
levisLog.WriteEntry("File Watch Type: " + ChangeType);
if (ChangeType == "Created")
{
levisLog.WriteEntry("File: " + e.FullPath + " " + e.Name + " Created");
}
else if (ChangeType == "Deleted")
{
levisLog.WriteEntry("File: " + e.FullPath + " " + e.Name + " Deleted");
}
else if (ChangeType == "Changed")
{
levisLog.WriteEntry("File: " + e.FullPath + " " + e.Name + " Changed");
}
}