2016-03-01 270 views
0

从本质上讲就是我想要做的是能够获得一个字符串读命令进入命令行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命令行提供的任何命令迂回的方式。我的目标是远程访问命令行并能够发送任何命令。有人可以帮我解决这个问题吗?

+0

你想运行'cmd/c shutdown -s'来关闭... shutdown是cmd里面的命令,而不是它本身的可执行文件。 – Haukinger

+0

我相信这里已经回答了这个问题; http://stackoverflow.com/questions/1469764/run-command-prompt-commands –

回答

0

可以使用Process类开始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); 
+0

好吧,我做了这个,但是当我发送它的命令时,它将显示标签为“更多?”的服务器的文件路径。这是什么意思? – Joris

+0

对不起,但我不明白你的意思,你使用了什么命令? –

+0

这张图片将解释它。 http://imgur.com/CJE7UDP左边是我的输入,右边是它带来的字符串“More?” – Joris