Can not use tskill in C #

3

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

  

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


However the same command when running via the command prompt works without any problem. I figured maybe this could happen through Visual Studio using its own directory to enable access to the Windows command lines, so I set up another working directory to it, as can be seen. However, still unsuccessful.

I checked that this question has already been done, though the answer does not serves.

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

Thanks for any help.

    
asked by anonymous 05.10.2018 / 15:47

1 answer

1

From what I understand you are trying to kill a process, if so why not do so:

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

Or, as Ricardo Pontual suggested using tskill.exe instead of cmd to run 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());
}
    
05.10.2018 / 16:33