简体   繁体   中英

C# WebClient.UploadFileAsync Cannot access the file because it's being used by another process

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 ! :)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM