【问题标题】:FileSystemWatcher not monitoring local user folder or temporary internet files folder in Vista (64bit)FileSystemWatcher 不监视 Vista(64 位)中的本地用户文件夹或临时 Internet 文件夹
【发布时间】:2023-03-23 06:36:01
【问题描述】:

我编写了一个测试程序来监视我的图片文件夹,该文件夹指向同一用户的 c:\users[username]\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(); 
            }
        }

    }


}

【问题讨论】:

  • 你检查过文件夹权限吗?
  • 有可能被Vista/Win7虚拟化了吗?当您运行提升的程序时会发生什么?
  • 文件夹权限在我删除的 c:\users 上是只读的,但没有帮助。不想运行虚拟PC。我认为应该有某种方法可以像访问其他文件夹一样访问所有系统文件夹,因为该程序在其他文件夹上运行完美,所以它是一个权限问题。我什至尝试以管理员身份和 32 位应用程序运行。
  • 感谢 Alex,这个工具真的很有帮助。我在我的代码周围放置了 try catch 以缩小问题的范围。现在它在 mypicture 文件夹中工作。虽然当我尝试 Filemon 时,我得到消息说 processmon 是最新的,应该改用。无论如何,感谢您的帮助。非常感激。我已经包含了上面的工作代码,以防有人发现它有用。

标签: c# windows-vista filesystemwatcher


【解决方案1】:

尝试运行 FileMon(可通过 MSDN 获得 SysInternals 工具)。它将向您展示您的代码在文件系统上的实际作用。然后,当您将代码指向“我的图片”等时,您也许能够找出行为不同的原因或确切的原因。

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 2013-01-18
    • 2017-12-19
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多