Process.Start(...) 抛出 "The system cannot find the file specified"

Process.Start(...) throws "The system cannot find the file specified"

为了在不重新启动计算机的情况下立即更改注册表,我发现使用

cmd.exe /c taskkill.exe /f /im explorer.exe & explorer.exe

正是我想要的。

我读到你不能在没有完整路径的情况下使用像 cmd.exe 这样的文件,因为它们没有 PATH 值并且不存在于System32 文件夹。

const string explorer = @"C:\Windows\explorer.exe";
string taskkill = "", commandprompt = "";
var task1 = Task.Run(() =>
    taskkill = Directory.GetDirectories(@"C:\Windows\WinSxS", "*microsoft-windows-taskkill_*")[0] + @"\taskkill.exe");
var task2 = Task.Run(() =>
    commandprompt = Directory.GetDirectories(@"C:\Windows\WinSxS", "*microsoft-windows-commandprompt_*")[0] + @"\cmd.exe");

Task.WaitAll(task1, task2);
Process.Start(string.Format($"{commandprompt} /c {taskkill} /f /im {explorer} & {explorer}"));

但是运行这段代码抛出

"The system cannot find the file specified"

如果有人能帮我解决这个问题,我将不胜感激!

编辑 #1:

Process.Start(commandprompt, string.Format($"/c {taskkill} /f /im {explorer} & {explorer}"));

通过更改回答的代码,命令提示符只打开一秒钟,然后回答类似“请求无效”,然后浏览器window 打开。

如果你想传递命令行参数,你需要使用 the two-argument overload of Process.Start

您不必调用 cmd.exe /c,您应该可以直接 运行 taskkill.exe。

这在我的机器上有效(windows 10)。您需要每次都搜索文件吗?我认为对于一个简单的实用程序应用程序,硬编码路径应该没问题。

var startInfo = new ProcessStartInfo()
{
    Verb = "runas",
    Arguments = "/f /im explorer.exe",
    FileName = @"c:\windows\system32\taskkill.exe"
};
var process = new Process { StartInfo = startInfo };
process.Start();
process.WaitForExit();
startInfo = new ProcessStartInfo()
{
    Verb = "runas",
    FileName = @"C:\windows\explorer.exe"
};
process = new Process { StartInfo = startInfo };
process.Start();