2016-07-28 29 views
5

我有一个更新程序,一旦检测到更新(从远程XML文件),通过主程序调用它,首先检查程序是否打开我的更新程序未能关闭我的主程序(C#)

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

    clsProcess.CloseMainWindow(); 
    return true; 

} 

(这被运行的每个进程,直到它找到它(foreach循环))

的更新,然后下载文件:

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

,然后它试图删除当前和R易名是:

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

,但我得到这个时候发生错误是:

未处理的异常:System.UnauthorizedAccessException:对路径“d:\ Conkinator的Skype工具\ Conkinator- Bot.exe'被拒绝。

但是该程序的新版本下载。

+0

尝试运行您的更新过程管理员 – Glubus

+0

@Glubus当我这样做,大部分时间它仍然无法正常工作,有时它。 –

+0

尝试在下载文件之前添加一些延迟,因为可能您的过程需要一点时间来关闭自己 –

回答

6

仅仅因为主窗口关闭并不意味着该过程结束。你需要等待进程退出关闭主窗口后:

clsProcess.WaitForExit(); 

理想情况下,你会使用超时 - 有可能是一些防止窗口关闭,或该进程可能有一个错误的出口机制。

+0

这个工作,当我测试它只是现在:)谢谢 –

2

从主程序本身关闭主程序要容易得多。

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