btald1331
btald1331

Reputation: 597

System File Watcher C# locking Directory?

I have a system file watcher monitoring the Downloads directory of the user in C#.The program is a windows service and each time it finds a file that has been dropped into or downloaded into the downloads directory the files are moved to another directory. As the service is running, when a download attempts, it will fail. Chrome just says Failed - Download error, without description of what the error is. I can only assume my windows service is locking the directory by watching it, so nothing can be downloaded to it. When the windows service is turned off, I can download normally again. I'm pretty new to C#, so I am unfamiliar with common errors, but here's my code:

    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;
                    }
                }


            }    
        }
    }

UPDATE I tried filtering out the temporary download extensions before moving but I still seem to get the error. Here is my attempt:

    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;
                        }
                    }
                }                    
            }    
        }
    }

Upvotes: 1

Views: 837

Answers (1)

Nik Bo
Nik Bo

Reputation: 1410

When you download something with Chrome it creates an Unconfirmed file like this: Unconfirmed 50329.crdownload

I tested it manually on my system by dragging this file away, result -> Failed to Download - Error

I think that your Service is moving this file, before Chrome can finish the download.

EDIT:

For the example with Chrome download files you could check the file extension like this:

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

MSDN FileSystemInfo.Extension

EDIT2: Use instead of the Array a List:

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

Then you can check your extension like this:

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

Make sure that you are using System.Linq;

On this way you can add some new extensions without an if about countless rows ;)

Upvotes: 6

Related Questions