Cannot use tskill in C#

Asked

Viewed 279 times

3

The tskill command when used via C# (Visual Studio) returns this error:

'tskill' not recognized as an internal or external command, a operable program or a batch file


However the same command when executing via command prompt works without any problem. I thought that maybe this could happen by Visual Studio using a directory to enable access to the Windows command lines, so I set up another working directory to it, as can be seen. Tedious, yet unsuccessful.

I found that that question has already been made, but the answer does not suit me.

private void ClosePorts(object sender, EventArgs e)
{            
    var processStartInfo = new ProcessStartInfo();
    processStartInfo.WorkingDirectory = (@"C:\Windows\System32\");
    processStartInfo.FileName = ("cmd.exe");     
    processStartInfo.Arguments = string.Format("/c {0}", string.Concat(@"tskill com2tcp"));
    processStartInfo.RedirectStandardInput = true;
    processStartInfo.RedirectStandardOutput = true;
    processStartInfo.UseShellExecute = false;
    processStartInfo.LoadUserProfile = true;
    processStartInfo.Verb = @"runas";
    processStartInfo.CreateNoWindow = true;    

    Process proc = Process.Start(processStartInfo);

    Console.WriteLine(proc.StandardOutput.ReadToEnd());
}

I appreciate any help.

  • why not execute the command with the full path? "C:\Windows\System32\tskill com2tcp"

  • The same way it doesn’t work, I’ve tested it this way.

1 answer

2


From what I understand you’re trying to kill a lawsuit, if yes why not do so:

foreach(var process in Process.GetProcessesByName("com2tcp"))
    process.Kill();

Or, like the Ricardo Pontual suggested, use tskill.exe directly instead of cmd to execute the command:

private void ClosePorts(object sender, EventArgs e)
{            
    var processStartInfo = new ProcessStartInfo();
    processStartInfo.WorkingDirectory = (@"C:\Windows\System32\");
    processStartInfo.FileName = "tskill.exe";     
    processStartInfo.Arguments = "com2tcp";
    processStartInfo.RedirectStandardInput = true;
    processStartInfo.RedirectStandardOutput = true;
    processStartInfo.UseShellExecute = false;
    processStartInfo.LoadUserProfile = true;
    processStartInfo.Verb = @"runas";
    processStartInfo.CreateNoWindow = true;    

    Process proc = Process.Start(processStartInfo);

    Console.WriteLine(proc.StandardOutput.ReadToEnd());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.