从 WinForms 应用程序延迟执行 cmd.exe 命令

问题描述 投票:0回答:1

我正在开发一个非常简单的桌面应用程序,需要自行自动更新,我无法使用 ClickOnce,因为安装路径必须始终相同(即 C:/app/myapp.exe)。

我的应用程序正确检查新的可用版本,将文件下载到临时文件夹中,并在延迟 N 秒后删除自己的 .exe 文件并将其替换为新文件。

我正在使用 System.Diagnostics.ProcessStartInfo 来实现我的目的。这是我的工作代码的简化版本:

ProcessStartInfo testProcess = new ProcessStartInfo();
testProcess.Arguments = 
                    "/C choice /C Y /N /D Y /T 5 & cd \"" + "C:/myapp" + "\" " + 
                    "& del \"" + "hello.txt" + "\" & choice";
testProcess.WindowStyle = ProcessWindowStyle.Normal;
testProcess.ErrorDialog = true;
testProcess.CreateNoWindow = false;
testProcess.FileName = "cmd.exe";
Process.Start(testProcess);

我可以正确删除文件、移动、替换文件等。当我尝试启动新的 .exe 时,问题就出现了。代码非常相似,它仅更改我正在使用的 cmd 命令(启动)以运行新应用程序,这是该部分的完整代码:

ProcessStartInfo testProcess = new ProcessStartInfo();
procesoTest.Arguments = 
                    "/C choice /C Y /N /D Y /T 5 & cd \"" + "C:/myapp" + "\" " + 
                    "& start \"" + "myapp.exe" + "\" & choice";
testProcess.WindowStyle = ProcessWindowStyle.Normal;
testProcess.ErrorDialog = true;
testProcess.CreateNoWindow = false;
testProcess.FileName = "cmd.exe";
Process.Start(testProcess);

.exe 存在于 C:/myapp/myapp.exe 中,但是当我尝试通过命令启动它时,我只得到一个新的 cmd.exe 控制台窗口,标题为“myapp.exe”,并且没有任何运行。

c# .net file cmd process
1个回答
0
投票

运行

start /?
,你就会知道启动命令后引用的参数就是标题。如果您想运行 myapp.exe,请删除引号或在 myapp.exe 之前添加标题参数。

"& start " + "myapp.exe" + " & choice";
"& start \"MYAPP\" \"" + "myapp.exe" + "\" & choice";
© www.soinside.com 2019 - 2024. All rights reserved.