将命令读入命令行C#
Reading commands into the command line C#
基本上我想做的是能够接受一个字符串
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);
并让应用程序将其放入命令行本身,而无需用户执行。就我所做的研究而言,我找不到直接这样做的方法。我找到了一个迂回的方式来执行此操作
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;
}
但这似乎不起作用,而且它也不允许您执行 windows 命令行中可用的任何命令。我的目标是远程访问命令行并能够向其发送任何命令。有人可以帮我解决这个问题吗?
您可以使用 Process
class 启动 cmd
应用程序,并将输入重定向到 Process.StandardInput
以便您能够在控制台中执行命令:
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.UseShellExecute = false;
info.RedirectStandardInput = true;
var process = Process.Start(info);
并这样使用:
string command = "shutdown -s";
process.StandardInput.WriteLine(command);
基本上我想做的是能够接受一个字符串
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);
并让应用程序将其放入命令行本身,而无需用户执行。就我所做的研究而言,我找不到直接这样做的方法。我找到了一个迂回的方式来执行此操作
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;
}
但这似乎不起作用,而且它也不允许您执行 windows 命令行中可用的任何命令。我的目标是远程访问命令行并能够向其发送任何命令。有人可以帮我解决这个问题吗?
您可以使用 Process
class 启动 cmd
应用程序,并将输入重定向到 Process.StandardInput
以便您能够在控制台中执行命令:
ProcessStartInfo info = new ProcessStartInfo("cmd.exe");
info.UseShellExecute = false;
info.RedirectStandardInput = true;
var process = Process.Start(info);
并这样使用:
string command = "shutdown -s";
process.StandardInput.WriteLine(command);