积极答复者
新程序覆盖原来正在运行的程序并重新启动的问题

问题
-
由于原程序在运行,所以想先把原程序进程kill
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
foreach (Process process in processes)
{
if (process.Id != currentProcess.Id)
{
process.Kill();
process.WaitForExit();
}
}System.IO.File.Copy(currentFile, targetFile, true);
System.Diagnostics.Process.Start(targetFile);
this.Close();
Application.Exit();
可是在执行替换时还是提示程序正在运行,无法替换;另外File.Copy后面的语句会等拷贝完成后再执行么?
答案
-
你好!KILL 后不一定马上关掉,你可以等一些时间再执行拷贝。
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
foreach (Process process in processes)
{
if (process.Id != currentProcess.Id)
{
process.Kill();
process.WaitForExit();
}
}Thread.Sleep(500);
System.IO.File.Copy(currentFile, targetFile, true);
System.Diagnostics.Process.Start(targetFile);
this.Close();
Application.Exit();
知识改变命运,奋斗成就人生!- 已标记为答案 ahking 2009年12月29日 8:18
-
Process currentProcess = Process.GetCurrentProcess();Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);foreach (Process process in processes){if (process.Id != currentProcess.Id)process.Kill();}Thread.Sleep(20);int tryCount = 0;// 失败了再尝试,最多尝试 10 次while (true){try{System.IO.File.Copy(currentFile, targetFile, true);System.Diagnostics.Process.Start(targetFile);break;}catch{Thread.Sleep(20);}tryCount++;if (tryCount > 10) // 最多尝试 10 次throw new Exception("....");}
知识改变命运,奋斗成就人生!- 已标记为答案 ahking 2009年12月29日 8:18
-
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
foreach (Process process in processes)
{
if (process.Id != currentProcess.Id)
{
process.Kill();
while(!process.WaitForExit(60000))
{
continue;
}
System.IO.File.Copy(currentFile, targetFile, true);
System.Diagnostics.Process.Start(targetFile);
}
}
this.Close();
Application.Exit();
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~- 已标记为答案 ahking 2009年12月29日 8:18
全部回复
-
你好!KILL 后不一定马上关掉,你可以等一些时间再执行拷贝。
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
foreach (Process process in processes)
{
if (process.Id != currentProcess.Id)
{
process.Kill();
process.WaitForExit();
}
}Thread.Sleep(500);
System.IO.File.Copy(currentFile, targetFile, true);
System.Diagnostics.Process.Start(targetFile);
this.Close();
Application.Exit();
知识改变命运,奋斗成就人生!- 已标记为答案 ahking 2009年12月29日 8:18
-
你好!
Thread.Sleep(500);KILL 后不一定马上关掉,你可以等一些时间再执行拷贝。Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
foreach (Process process in processes)
{
if (process.Id != currentProcess.Id)
{
process.Kill();
process.WaitForExit();
}
}Thread.Sleep(500);
System.IO.File.Copy(currentFile, targetFile, true);
System.Diagnostics.Process.Start(targetFile);
this.Close();
Application.Exit();
知识改变命运,奋斗成就人生!
可是这个时间如何控制呢,如果500ms也没关掉呢,process.WaitForExit();不起作用?有没有判断结束的方法啊?
另外File.Copy后面的语句会等拷贝完成后再执行么? -
你这样写,你的方法并没有把当前线程杀死
System.Diagnostics.Process[] myProcesses = System.Diagnostics.Process.GetProcesses();Process currentProcess = Process.GetCurrentProcess();
foreach (System.Diagnostics.Process myProcess in myProcesses)
{
if (currentProcess.ProcessName == myProcess.ProcessName)
myProcess.Kill();
}
努力+方法=成功 -
Process currentProcess = Process.GetCurrentProcess();Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);foreach (Process process in processes){if (process.Id != currentProcess.Id)process.Kill();}Thread.Sleep(20);int tryCount = 0;// 失败了再尝试,最多尝试 10 次while (true){try{System.IO.File.Copy(currentFile, targetFile, true);System.Diagnostics.Process.Start(targetFile);break;}catch{Thread.Sleep(20);}tryCount++;if (tryCount > 10) // 最多尝试 10 次throw new Exception("....");}
知识改变命运,奋斗成就人生!- 已标记为答案 ahking 2009年12月29日 8:18
-
Process currentProcess = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(currentProcess.ProcessName);
foreach (Process process in processes)
{
if (process.Id != currentProcess.Id)
{
process.Kill();
while(!process.WaitForExit(60000))
{
continue;
}
System.IO.File.Copy(currentFile, targetFile, true);
System.Diagnostics.Process.Start(targetFile);
}
}
this.Close();
Application.Exit();
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~- 已标记为答案 ahking 2009年12月29日 8:18