简体   繁体   中英

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?

Use Process.GetProcessesByName and Process.Kill :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM