简体   繁体   中英

My updater is failing to close my main program (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.

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.

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

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