My updater cannot close my main program (C #)

I have an updater that is called through the main program after detecting the update (from a remote XML file), it first checks if the process is open

if (clsProcess.ProcessName.ToLower().Contains("conkinator-bot.exe")) { clsProcess.CloseMainWindow(); return true; } 

(this starts for each process until it finds it (foreach loop))

the updater downloads the file:

 client.DownloadFile(url, "Conkinator-Bot-new.exe"); 

and then he tries to delete the current one and rename it:

 File.Delete("Conkinator-Bot.exe"); File.Move("Conkinator-Bot-new.exe", "Conkinator-Bot.exe"); 

but the error I get when this happens is the following:

Unhandled exception: System.UnauthorizedAccessException: denied access to the path "D: \ Conkinator Skype Tool \ Conkinator-Bot.exe".

however, a new version of the program is being downloaded.

+5
source share
2 answers

Just because the main window is closed does not mean that the process is complete. You need to wait for the process to complete after closing the main window:

 clsProcess.WaitForExit(); 

Ideally, you should use a timeout - there may be something that prevents the window from closing, or the process may have a faulty exit mechanism.

+6
source

It is much easier to close the main program from the main program itself.

  string msg = "To update the application we need to close it. Do you want to continue?"; if (DialogResult.Yes == MessageBox.Show(msg, title, MessageBoxButtons.YesNo)) { ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = "YourUpdaterFile.exe"; psi.WindowStyle = ProcessWindowStyle.Normal; // Assuming a lot here but to just show the options available.... psi.WorkingDirectory = Path.GetDirectoryName(Application.ExecutablePath); Process.Start(psi); Application.Exit(); } 
+2
source

All Articles