Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.Kill() returns access denied

Tags:

c#

process

I'm trying to kill a process using a CMD command line from my C# application but when i start the application nothing happens... When i try to kill from command prompt i recieve the message: "Access denied". I've tried to run my app as Administrator and the process was killed. How can i manage not to always use "Run as Administrator"?

Code:

System.Diagnostics.Process process = new System.Diagnostics.Process();
            System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
            startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = "/C taskkill /F /IM APP.EXE";
            process.StartInfo = startInfo;
            process.Start();
like image 728
Yildirim Avatar asked Feb 11 '26 15:02

Yildirim


1 Answers

Try this

System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.UserName = "Administrator";
        startInfo.Password = <password>;
        startInfo.Arguments = "/C taskkill /F /IM APP.EXE";
        process.StartInfo = startInfo;
        process.Start();
like image 177
mybirthname Avatar answered Feb 14 '26 05:02

mybirthname



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!