系统文件监视程序C#锁定目录

本文关键字:锁定 监视 程序 系统文件 | 更新日期: 2023-09-27 18:04:27

我有一个系统文件观察器在C#中监视用户的下载目录。该程序是一个windows服务,每次它发现一个文件被放入或下载到下载目录时,文件都会被移动到另一个目录。由于服务正在运行,当尝试下载时,它将失败。Chrome只是说Failed - Download error,没有说明错误是什么。我只能通过观察来假设我的windows服务正在锁定目录,所以什么都不能下载到它。当windows服务关闭时,我可以再次正常下载。我是C#的新手,所以我不熟悉常见的错误,但这是我的代码:

    private void watch()
    {
        string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
        path = Path.Combine(pathUser, "Downloads");
        watcher.Path = path;
        watcher.NotifyFilter = NotifyFilters.LastWrite
                     | NotifyFilters.FileName;
        watcher.Filter = "*.*";
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.EnableRaisingEvents = true;
    }
    private void OnChanged(object source, FileSystemEventArgs e)
    {
        String pUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
        String directoryName = Destination;
        DirectoryInfo dirInfo = new DirectoryInfo(directoryName);
        if (dirInfo.Exists == false)
            Directory.CreateDirectory(directoryName);
        List<String> MyFiles = Directory
                           .GetFiles(path, "*.*", SearchOption.AllDirectories).ToList();
        foreach (string file in MyFiles)
        {
            FileInfo mFile = new FileInfo(file);
            // to remove name collusion
            if (new FileInfo(dirInfo + "''" + mFile.Name).Exists == false)
            {
                bool flag = true;
                while (flag)
                {
                    if (!IsFileLocked(file, 1))
                    {
                        mFile.MoveTo(dirInfo + "''" + mFile.Name);
                        flag = false;
                    }
                }

            }    
        }
    }

更新我试着在移动之前过滤掉临时下载扩展,但我似乎仍然收到了错误。这是我的尝试:

    String[] ignoreExt = { "crdownload","download", "temp", "tmp"};
    private void OnChanged(object source, FileSystemEventArgs e)
    {
        String pUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
        String directoryName = Destination;
        DirectoryInfo dirInfo = new DirectoryInfo(directoryName);
        if (dirInfo.Exists == false)
            Directory.CreateDirectory(directoryName);
        List<String> MyFiles = Directory
                           .GetFiles(path, "*.*", SearchOption.AllDirectories).ToList();
        foreach (string file in MyFiles)
        {
            FileInfo mFile = new FileInfo(file);
            // to remove name collusion
            if (new FileInfo(dirInfo + "''" + mFile.Name).Exists == false)
            {
                bool flag = true;
                while (flag)
                {
                    if (!IsFileLocked(file, 1))
                    {
                        if (mFile.Extension != ignoreExt[0] || mFile.Extension != ignoreExt[1] || mFile.Extension != ignoreExt[2] || mFile.Extension != ignoreExt[3])
                        {
                            mFile.MoveTo(dirInfo + "''" + mFile.Name);
                           // Clipboard.SetText(dirInfo + "''" + mFile.Name);
                            flag = false;
                        }
                    }
                }                    
            }    
        }
    }

系统文件监视程序C#锁定目录

当你用Chrome下载一些东西时,它会创建一个Unconfirmed文件,如下所示:Unconfirmed 50329.crdownload

我把这个文件拖走,在我的系统上手动测试了它,结果->Failed to Download - Error

我认为在Chrome完成下载之前,您的服务正在移动此文件。

编辑:

对于Chrome下载文件的示例,您可以检查如下文件扩展名:

if(mFile.Extension != "crdownload")
{
    ...
}

MSDN文件系统信息扩展

第2版:使用数组列表代替:

List<String> ignoreExt = new  List<String>() { "crdownload","download", "temp", "tmp"};

然后你可以这样检查你的分机:

if (ignoreExt.Where(x => x == mFile.Extension).ToList().Count == 0)
{
    mFile.MoveTo(dirInfo + "''" + mFile.Name);
   // Clipboard.SetText(dirInfo + "''" + mFile.Name);
    flag = false;
}

确保你是using System.Linq;

通过这种方式,您可以添加一些新的扩展,而无需if about无数行;(