Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My updater is failing to close my main program (C#)

Tags:

c#

I have an updater, which is called via the main program once an update is detected (from a remote XML file), first it checks whether the process is open

if (clsProcess.ProcessName.ToLower().Contains("conkinator-bot.exe"))
{

    clsProcess.CloseMainWindow();
    return true;

}

(this gets run for every process until it finds it (foreach loop))

the updater then downloads the file:

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

and then it attempts 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 that I get when this occurs is the following:

Unhandled Exception: System.UnauthorizedAccessException: Access to the path 'D:\Conkinator's Skype Tool\Conkinator-Bot.exe' is denied.

however the new version of the program DOES download.

like image 413
Connor Wright Avatar asked Jul 28 '16 12:07

Connor Wright


Video Answer


2 Answers

Just because the main window is closed doesn't mean the process is over. You need to wait for the process to exit after you close the main window:

clsProcess.WaitForExit();

Ideally, you'd use a timeout - there might be something preventing the window from closing, or the process might have a faulty exit mechanism.

like image 79
Luaan Avatar answered Nov 15 '22 03:11

Luaan


It is a lot easier to close the main program from inside 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();
  }
like image 35
Steve Avatar answered Nov 15 '22 03:11

Steve