0

I have been trying to download a program off of a website and then run said program. When I run it I get the following exception. I am new to C# so please use simple language.

System.ComponentModel.Win32Exception: 'The process cannot access the file because it is being used by another process'

No other process is using this program and should run fine.

        if (restart == "1")
        {
            wc.DownloadFileCompleted += new 
            AsyncCompletedEventHandler(FileDowloadComplete);
            Uri imageurl = new Uri(Website);
            System.Threading.Thread.Sleep(1000);
            wc.DownloadFileAsync(imageurl, "Example.exe");
            System.Threading.Thread.Sleep(1000);
            System.Diagnostics.Process.Start("Example.exe");
            System.Threading.Thread.Sleep(1000);
        }

The download works fine and the program will run if I comment out wc.DownloadFileAsync(imageurl, "Example.exe");

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
Zach Generic Name
  • 115
  • 1
  • 1
  • 8
  • The file is being used by another progress. It says it right there in black and white. I assume you're running `Example.exe` at the same time as trying to overwrite it? – ProgrammingLlama Mar 11 '18 at 07:15
  • No. as I said nothing is reading/writing or monitering this program in any way. If i were to comment out `wc.DownloadFileAsync(imageurl, "Example.exe");` It will will work perfectly. I'm pretty sure it has something to do with that. – Zach Generic Name Mar 11 '18 at 07:46
  • Use [Process Explorer](https://learn.microsoft.com/en-us/sysinternals/downloads/process-explorer) to find out what is using `Example.exe`. The error message is not a red herring. It works when you comment out that line because you're no longer trying to write to the file with the `DownloadFileAsync` method so there is no chance to discover that the file is already in use! – ProgrammingLlama Mar 11 '18 at 07:47
  • By the way, if this wasn't already abundantly clear: Windows will prevent writing to Example.exe if Example.exe is a running process on the system. – ProgrammingLlama Mar 11 '18 at 07:49
  • john 85 Thanks for the advice. I made the program wait for an extended amount of time and it fixed it. – Zach Generic Name Mar 11 '18 at 07:58

2 Answers2

1

The code has problems. Firstly its using DownloadFileAsync which (as it says) downloads in the background. The code does not get any confirmation that the file has been downloaded - it just sleeps for 1 second and 'hopes' that thats enough time to download the file.

The code does register a FileDownloadComplete callback, but you havent included the code of that callback in your example; its very likely that its the source of your error. Can you post the complete example.

Because when you comment out the DownloadFileAsync you say your program runs fine, you must alredy have a copy of Example.exe present on your disk. DownloadFileAsync will overwrite an existing file - but only if its not locked. Whats happening is that the 1 second Sleep is not long enough for the Download to complete; your program then starts the existing Example.exe and locks it, and then the DownloadFileAsync completes and tries to copy the temporary downloaded file over the top of the existing Example.exe which you are already running and have in a locked state.

PhillipH
  • 6,182
  • 1
  • 15
  • 25
0

Example.exe is being written onto. To stop the error make the program wait for the Example.exe to finish downloading.

Zach Generic Name
  • 115
  • 1
  • 1
  • 8
  • No - this is an incorrect approach - you should not just 'wait longer' but wait until the event handler AsyncCompletedDownloadEventHandler calls your code to tell you that its completed. – PhillipH Mar 11 '18 at 10:43