当应用程序未处于活动状态时,FileSystemWatcher将失败

时间:2012-12-21 00:03:13

标签: windows-mobile filesystemwatcher opennetcf

我正在尝试在Windows Mobile 6.1设备上运行一个应用程序(用c#编写),在后台(带有filesystemwatcher)在另一个应用程序后面运行。我编写了一个简单的filesystemwatcher,只要它当前的应用程序(换句话说只要你不打开其他程序)就可以很好地工作。即使在返回实例后,filesystemwatcher也不再起作用。它使用OpenNETCF,因为.net cf不包含文件系统观察程序。

任何人都知道为什么这不起作用?

任何帮助将不胜感激。

这是我的代码:

    private void Form1_Load(object sender, EventArgs e)
    {

        OpenNETCF.IO.FileSystemWatcher f = new OpenNETCF.IO.FileSystemWatcher(@"c:\happydays", "*.*");
        f.IncludeSubdirectories = true;
        f.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite |
                       NotifyFilters.FileName | NotifyFilters.DirectoryName;

        f.Created += new FileSystemEventHandler(File_Created);                                  
        f.EnableRaisingEvents = true;
    }

    private void File_Created(object sender, OpenNETCF.IO.FileSystemEventArgs e)
    {
MessageBox.Show("File Created");
    }

1 个答案:

答案 0 :(得分:2)

我不确定你为什么会这样做,但我可能会猜测操作系统只将文件通知消息发送给活动应用程序。

SDF的FileSystemWatcher依赖于应用程序消息泵来运行,因为它使用了引擎盖下的aygshell API,并且aygshell使用Windows消息进行调度(Microsoft的选择,而不是我的选择)。我出于几个原因不喜欢它,所以我写了一个名为FileSystemMonitor的替代方案,你可以在同一个命名空间和DLL以及FileSystemWatcher中找到它。 IIRC,它没有100%相同的工作,但考虑到我必须使用的文件系统API,我尽可能接近它。

相关问题