0

I have a C# program. It is literally one line:

System.Diagnostics.Process.Start(@"C:\ProgramData\task manager\killtask.vbs");

There is a VBS file there, which generates a batch file that allows you to type a command and it will be executed and closed. It then sends the following keys: "tskill /a notepad {ENTER}". I know that's probably the worst practice you've ever seen, but bear with me.

When the VBS file is run by hand, it successfully closes notepad. When it is run through C# using the above line, it prints "tskill is not recognized" etc. before it closes.

Why is it that I can't use tskill through batch via VBS via C#, but I can use it just through batch via VBS? Remember, both clicking on it and running my C# code successfully ATTEMPT to kill notepad, but only clicking on it by hand closes notepad successfully.

tryashtar
  • 278
  • 3
  • 13
  • There are clearly more complicated ways to [kill a process in C#](http://stackoverflow.com/questions/1642231/how-to-kill-a-c-sharp-process), but this approach is close to top of "the most steps to perform simple operation". To complicate things you can add JavaScript and PowerShell into the chain :) – Alexei Levenkov Aug 24 '15 at 20:18
  • check if your windows distribution comes with terminal services.Some don't have and which also means no tskill. – npocmaka Aug 24 '15 at 20:36

1 Answers1

1

You probably need to set the working directory. Without it your program will be executed from the directory of the process that starts your script and this directory is not the correct one.

Juse try with

var processStartInfo = new ProcessStartInfo();
processStartInfo.WorkingDirectory = @"C:\ProgramData\task manager");
processStartInfo.FileName = "killtask.vbs";
Process proc = Process.Start(processStartInfo);
Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    That's probably the reason... Also 99% of scripts/cmd files fail to properly handle paths with spaces - so that could be another reason. – Alexei Levenkov Aug 24 '15 at 20:19