简体   繁体   English

将命令读入命令行C#

[英]Reading commands into the command line C#

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. 但这似乎不起作用,并且也不允许您执行Windows命令行中可用的任何命令。 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? 有人可以帮我这个忙吗?

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: 您可以使用Process类启动cmd应用程序,然后将输入重定向到Process.StandardInput以便能够在控制台中执行命令:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM