0

Essentially what I want to do is be able to take a string

byte[] RecPacket = new byte[1000];

//Read a command from the client.

Receiver.Read(RecPacket, 0, RecPacket.Length);

//Flush the receiver

Receiver.Flush();

//Convert the packet into a readable string

string Command = Encoding.ASCII.GetString(RecPacket);

and have the application put it into the command line itself without the user doing it. As far as the research I've done I cannot find a way to directly do that. I found a roundabout way in which you do this

switch (Command)
{
case "SHUTDOWN":

string shutdown = Command;

//Shuts it down

System.Diagnostics.Process SD = new System.Diagnostics.Process();

SD.StartInfo.FileName = "shutdown -s";

SD.Start();

break;

}

but that doesn't seem to work and it also doesn't allow you to do any command available in the windows command line. My goal is to remotely access the command line and be able to send any command to it. Can someone help me out with this?

Zach
  • 640
  • 1
  • 8
  • 20
  • You want to run `cmd /c shutdown -s` to shutdown... shutdown is a command inside cmd, not an executable by itself. – Haukinger Mar 01 '16 at 19:30
  • I belive the question has already been answered here; http://stackoverflow.com/questions/1469764/run-command-prompt-commands – Kjetil Klaussen Mar 01 '16 at 19:35

1 Answers1

0

You can start a cmd application using Process class, and redirect the input to Process.StandardInput so you will be able to execute commands in the console:

ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.UseShellExecute = false;
info.RedirectStandardInput = true;

var process = Process.Start(info);

And use it in this way:

string command = "shutdown -s";
process.StandardInput.WriteLine(command);
Arturo Menchaca
  • 15,783
  • 1
  • 29
  • 53
  • Ok I did this but when I send it the command it will display the file path of the server with the tag "More?" what does this mean? – Zach Mar 01 '16 at 20:53
  • sorry but i don't gets what you mean, what command are you using? – Arturo Menchaca Mar 01 '16 at 20:57
  • This image will explain it. http://imgur.com/CJE7UDP On the left is my input and on the right is what it brings up – Zach Mar 01 '16 at 21:20