0

I'm building a simple application that synchronizes a folder on the computer with a remote server. I use FileSystelWatcher to detect changes and WebClient to upload the file. The problem is that when I try to upload I get an exception saying the file is already in use. I checked all the processes but I can't find the one that is blocking. I also tried this File access error with FileSystemWatcher when multiple files are added to a directory but the problem remains the same. Here is how I declare the FileSysemWatcher:

private void InitFileWatcher()
    {
        try
        {
            watcher = new FileSystemWatcher();
            watcher.Path = FileManager.getSyncDirPath();
            watcher.EnableRaisingEvents = true;
            watcher.IncludeSubdirectories = true;
            watcher.Created += FileManager.FileCreated;
        }
        catch(Exception)
        {
            Console.WriteLine("Error");
        }
    }

FileManager.FileCreated method:

public static void FileCreated(object sender, FileSystemEventArgs e) 
    {
        Console.WriteLine("File created: " + e.FullPath);
        while (true)
        {
            try
            {
                using (Stream stream = System.IO.File.Open(e.FullPath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                {
                    if (stream != null)
                    {
                        FileUploader uploader = new FileUploader();
                        uploader.Upload(e.FullPath, new Uri("http://192.168.65.1/test/upload.php"));
                        break;
                    }
                }
            }
            catch (FileNotFoundException ex)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", e.FullPath, ex.Message));
            }
            catch (IOException ex)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", e.FullPath, ex.Message));
            }
            catch (UnauthorizedAccessException ex)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", e.FullPath, ex.Message));
            }
            Thread.Sleep(500);
        }
    }

And finally the upload method:

public void Upload(string file, Uri destination)
    {
        WebClient client = new WebClient();
        client.Headers.Add("Content-Type", "binary/octet-stream");
        client.UploadFileAsync(destination, "POST", file);
    }

Thanks for the help ! :)

2 Answers 2

1

It won't let you upload the file because the file is already opened using System.IO.File.Open

If you are opening the file just to check its existence, then you can use

File.Exists(path);
Sign up to request clarification or add additional context in comments.

4 Comments

I tried both of the solutions combined and it works better but it's very unstable. And it doesn't work with "big" files (> 10Mo I think)
What do you mean by unstable?
It's a System.Reflection.TargetInvocationException. The same that I always have. And the innerException says the file can't be accessed.
And by unstable I mean that on 5 attempts to upload, 3 have failed. Even for small files.
0

You have to try below mentioned code.

    public void Upload(string file, Uri destination)
    {
       using (WebClient client=new Webclient())
        {
           client.Headers.Add("Content-Type", "binary/octet-stream");
           client.UploadFileAsync(destination, "POST", file);
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.