Martin Niederl
Martin Niederl

Reputation: 759

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

For immediate registry change without restarting the computer i found out that using

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

is exactly doing what i want.

I read that you can't use files like cmd.exe without their whole path because they don't have a PATH value and don't exist in the System32 folder.

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

But running this piece of code throws

"The system cannot find the file specified"

Would appreciate it if someone could help me solve this problem!

EDIT #1:

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

by changing the code as answered, the command-prompt opens only for a second ans says something like "The request is invalid" and after that the explorer window opens.

Upvotes: 2

Views: 7077

Answers (2)

Miniver Cheevy
Miniver Cheevy

Reputation: 1677

You shouldn't have to call cmd.exe /c, you should be able to run taskkill.exe directly.

This works on my machine (windows 10).Do you need to search for the files every time? I think for a simple utility app, having the paths hard coded should be fine.

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

Upvotes: 2

You need to use the two-argument overload of Process.Start if you want to pass command-line parameters.

Upvotes: 1

Related Questions