Tom Lenc
Tom Lenc

Reputation: 765

Can't kill a process with name with spaces

I got this in my Main Class(the program is a patcher exe..):

private void Form1_Load(object sender, EventArgs e)
{

    // in the exe.dat is written this(the name of the running exe file): KF2 DSM.exe
    string FileName = File.ReadAllText("exe.dat"); 

    // this SHOULD kill the process BUT it doesn't! btw i also treid this: Process.Start("taskkill", "/F /IM " + '"' + FileName + '"');, and still nothing
    Process.Start("taskkill", "/F /IM " + FileName); 

    File.Delete(FileName);

    using (var client = new WebClient())
    {
        client.DownloadFile("https://onedrive.live.com/download?resid=763D7D60E7D1759D!328&authkey=!AArR3IwAehnZ3gc&ithint=file%2cexe", FileName);

        while(client.IsBusy)
        {
            Thread.Sleep(500);
        }
    }

    File.Delete("exe.dat");

    Process.Start(FileName);
}

I added some notes in the code for you.

I tried almost EVERY syntaxes/codes for killing a process, but none of them worked!

Is there another way to kill a process that would work for me?

Upvotes: 0

Views: 1010

Answers (1)

Blorgbeard
Blorgbeard

Reputation: 103467

Use Process.GetProcessesByName and Process.Kill:

foreach (var process in Process.GetProcessesByName(FileName)) {
    process.Kill();
}

Upvotes: 1

Related Questions