FileSystemWatcher 不监视 Vista(64 位)中的本地用户文件夹或临时 Internet 文件文件夹
我编写了一个测试程序来监视我的图片文件夹,该文件夹指向同一用户的 c:\users[用户名]\Pictures 和临时 Internet 文件文件夹。如果我将文件夹更改为其他位置(例如 d:\persona_pics),则该程序可以正常工作。 知道为什么当我设置要监视的提到的文件夹时没有引发事件吗?
这是代码。
class Program
{
static void Main(string[] args)
{
//FileSystemWatcher myJpegFileWatcher = new FileSystemWatcher(@"C:\Users\[username]\AppData\Local\Microsoft\Windows\Temporary Internet Files\low\content.ie5\");
FileSystemWatcher myJpegFileWatcher = new FileSystemWatcher(@"C:\Users\[username]\Pictures\ ");
myJpegFileWatcher.Filter = "*.jpg";
myJpegFileWatcher.Created += new FileSystemEventHandler(myJpegFileWatcher_Created);
myJpegFileWatcher.Changed += new FileSystemEventHandler(myJpegFileWatcher_Changed);
myJpegFileWatcher.IncludeSubdirectories = true;
myJpegFileWatcher.NotifyFilter = NotifyFilters.CreationTime;
myJpegFileWatcher.EnableRaisingEvents = true;
Console.Read();
}
static void myJpegFileWatcher_Changed(object sender, FileSystemEventArgs e)
{
FileInfo duplicateFile = new FileInfo(@e.FullPath);
bool flag = true;
while (flag)
{
try
{
if (duplicateFile.Length > 20000)
{
duplicateFile.CopyTo(@"d:\pics\spy\ " + e.Name);
flag = false;
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("file is being copied:{0}, Size={1}", e.FullPath, duplicateFile.Length);
fs.Close();
}
else
{
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("file is not being copied:{0}, Size={1}", e.FullPath, duplicateFile.Length);
fs.Close();
}
}
catch (Exception ex)
{
//
}
}
}
static void myJpegFileWatcher_Created(object sender, FileSystemEventArgs e)
{
FileInfo duplicateFile = new FileInfo(@e.FullPath);
bool flag = true;
while (flag)
{
try
{
if (duplicateFile.Length > 20000)
{
duplicateFile.CopyTo(@"d:\pics\spy\ " + e.Name);
flag = false;
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("file is being copied:{0}, Size={1}", e.FullPath, duplicateFile.Length);
fs.Close();
}
}
catch (Exception ex)
{
//
}
}
}
}
工作代码..
类程序 { 静态无效主(字符串[]参数) {
FileSystemWatcher myJpegFileWatcher = new FileSystemWatcher(@"C:\Users\[user]\Pictures\");
myJpegFileWatcher.Filter = "*.jpg";
myJpegFileWatcher.Changed += new FileSystemEventHandler(myJpegFileWatcher_Changed);
myJpegFileWatcher.IncludeSubdirectories = true;
myJpegFileWatcher.EnableRaisingEvents = true;
Console.Read();
}
static void myJpegFileWatcher_Changed(object sender, FileSystemEventArgs e)
{
FileInfo duplicateFile = new FileInfo(@e.FullPath);
bool flag = true;
while (flag)
{
try
{
if (duplicateFile.Exists)
{
if (duplicateFile.Length > 20000)
{
try
{
duplicateFile.CopyTo(@"d:\pics\spy\" + e.Name,true);
}
catch (Exception ex)
{
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("Error Inside copying:{0}", ex.Message);
fs.Close();
}
finally
{
flag = false;
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("file is being copied:{0}, Size={1}", e.FullPath, duplicateFile.Length);
fs.Close();
}
}
else
{
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("file is not being copied:{0}, Size={1}", e.FullPath, duplicateFile.Length);
fs.Close();
}
}
}
catch (Exception ex)
{
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("Error:{0}", ex.Message);
fs.Close();
}
}
}
}
I wrote a test program to monitor my Picture folder which points to c:\users[username]\Pictures and temporary internet files folder for the same user. This is program works perfectly fine if I change the folder to other location like d:\persona_pics.
Any idea why events are not being raised when I set the mentioned folder to monitor?
here is the code.
class Program
{
static void Main(string[] args)
{
//FileSystemWatcher myJpegFileWatcher = new FileSystemWatcher(@"C:\Users\[username]\AppData\Local\Microsoft\Windows\Temporary Internet Files\low\content.ie5\");
FileSystemWatcher myJpegFileWatcher = new FileSystemWatcher(@"C:\Users\[username]\Pictures\ ");
myJpegFileWatcher.Filter = "*.jpg";
myJpegFileWatcher.Created += new FileSystemEventHandler(myJpegFileWatcher_Created);
myJpegFileWatcher.Changed += new FileSystemEventHandler(myJpegFileWatcher_Changed);
myJpegFileWatcher.IncludeSubdirectories = true;
myJpegFileWatcher.NotifyFilter = NotifyFilters.CreationTime;
myJpegFileWatcher.EnableRaisingEvents = true;
Console.Read();
}
static void myJpegFileWatcher_Changed(object sender, FileSystemEventArgs e)
{
FileInfo duplicateFile = new FileInfo(@e.FullPath);
bool flag = true;
while (flag)
{
try
{
if (duplicateFile.Length > 20000)
{
duplicateFile.CopyTo(@"d:\pics\spy\ " + e.Name);
flag = false;
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("file is being copied:{0}, Size={1}", e.FullPath, duplicateFile.Length);
fs.Close();
}
else
{
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("file is not being copied:{0}, Size={1}", e.FullPath, duplicateFile.Length);
fs.Close();
}
}
catch (Exception ex)
{
//
}
}
}
static void myJpegFileWatcher_Created(object sender, FileSystemEventArgs e)
{
FileInfo duplicateFile = new FileInfo(@e.FullPath);
bool flag = true;
while (flag)
{
try
{
if (duplicateFile.Length > 20000)
{
duplicateFile.CopyTo(@"d:\pics\spy\ " + e.Name);
flag = false;
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("file is being copied:{0}, Size={1}", e.FullPath, duplicateFile.Length);
fs.Close();
}
}
catch (Exception ex)
{
//
}
}
}
}
Working code..
class Program
{
static void Main(string[] args)
{
FileSystemWatcher myJpegFileWatcher = new FileSystemWatcher(@"C:\Users\[user]\Pictures\");
myJpegFileWatcher.Filter = "*.jpg";
myJpegFileWatcher.Changed += new FileSystemEventHandler(myJpegFileWatcher_Changed);
myJpegFileWatcher.IncludeSubdirectories = true;
myJpegFileWatcher.EnableRaisingEvents = true;
Console.Read();
}
static void myJpegFileWatcher_Changed(object sender, FileSystemEventArgs e)
{
FileInfo duplicateFile = new FileInfo(@e.FullPath);
bool flag = true;
while (flag)
{
try
{
if (duplicateFile.Exists)
{
if (duplicateFile.Length > 20000)
{
try
{
duplicateFile.CopyTo(@"d:\pics\spy\" + e.Name,true);
}
catch (Exception ex)
{
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("Error Inside copying:{0}", ex.Message);
fs.Close();
}
finally
{
flag = false;
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("file is being copied:{0}, Size={1}", e.FullPath, duplicateFile.Length);
fs.Close();
}
}
else
{
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("file is not being copied:{0}, Size={1}", e.FullPath, duplicateFile.Length);
fs.Close();
}
}
}
catch (Exception ex)
{
StreamWriter fs = new StreamWriter(@"d:\pics\log.txt", true);
fs.WriteLine("Error:{0}", ex.Message);
fs.Close();
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试运行 FileMon(可通过 MSDN 获得的 SysInternals 工具)。它将向您显示您的代码在文件系统上实际执行的操作。然后,当您将代码指向“我的图片”等时,您也许能够找出为什么或到底有什么不同的行为。
Try to run FileMon (SysInternals tool available through MSDN). It will show you what your code actually does on the file system. Then you might be able to find out why or what exactly behaves differently when you point your code to "My Pictures" etc.