简体   繁体   English

在C#中使用命令提示符

[英]Working with the command prompt in C#

I am working on a project of remotely receiving commands from a server, but I am facing a problem when working with the command prompt locally. 我正在一个从服务器远程接收命令的项目,但是在本地使用命令提示符时遇到问题。 Once I get it working locally, then I will move to remote communication. 一旦它在本地运行,我将转向远程通信。

Problem: 问题:

  1. I have to completely hide the console, and client must not see any response when the client is working with the command line but it will show a console for a instance and then hide it. 我必须完全隐藏控制台,并且当客户端使用命令行时,客户端一定看不到任何响应,但是它将显示实例的控制台,然后将其隐藏。

  2. I had to use c# to send a command to cmd.exe and receive the result back in C#. 我必须使用c#将命令发送到cmd.exe并在C#中接收返回的结果。 I have done it in one way by setting the StandardOutput... and input to true. 我通过设置StandardOutput ...并将输入设置为true来完成此操作。

  3. The commands are not working. 命令不起作用。 For example, D: should change the directory to D and it does, but after that, if we use dir to see the directories in D, it does not show the appropriate directories. 例如, D:应该将目录更改为D,但确实如此,但是在此之后,如果我们使用dir查看D中的目录,则它不会显示适当的目录。

Here is my code: 这是我的代码:

First Method 第一种方法

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C " + textBoxInputCommand.Text + " >> " + " system";
process.StartInfo = startInfo;
process.Start();

Second Method 第二种方法

ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + textBoxInputCommand.Text);
procStartInfo.WorkingDirectory = @"c:\";
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = true;
procStartInfo.UseShellExecute = false;
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();
richTextBoxCommandOutput.Text += result;

I want the program to run as administrator because the exe it generates does not run commands when it runs from the C drive. 我希望程序以管理员身份运行,因为从C驱动器运行时,它生成的exe不运行命令。

  1. Try not to run the commands by passing them to cmd instead write the commands passed by the client to a .bat file execute the .bat. 尽量不要通过将命令传递给cmd来运行命令,而.bat客户端传递的命令写入.bat文件,然后执行.bat. file from your program this will probably hide your command prompt window. 文件从您的程序,这可能会隐藏您的命令提示符窗口。
  2. You can also use process.OutputDataRecieved event handler to do anything with the output. 您还可以使用process.OutputDataRecieved事件处理程序对输出执行任何操作。
  3. If you want to execute command using administrator rights you can use runas command. 如果要使用管理员权限执行命令,则可以使用runas命令。 It is equivalent to the sudo command in Linux . 它等效于Linuxsudo命令。 Here is a piece of code may be it will help you 这是一段代码,可能会对您有帮助

      var process = new Process(); var startinfo = new ProcessStartInfo(@"c:\\users\\Shashwat\\Desktop\\test.bat"); startinfo.RedirectStandardOutput = true; startinfo.UseShellExecute = false; process.StartInfo = startinfo; process.OutputDataRecieved += DoSomething; process.Start(); process.BeginOutputReadLine(); process.WaitForExit(); //Event Handler public void DoSomething(object sener, DataReceivedEventArgs args) { //Do something } 

    Hope it helps you. 希望对您有帮助。

You could hide command prompt window by adding this line of code: 您可以通过添加以下代码行来隐藏命令提示符窗口:

startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

or do not create it at all 根本不创建

startInfo.CreateNoWindow = true;

Here can be found a few awarding solutions: Run Command Prompt Commands 在这里可以找到一些奖励解决方案: 运行命令提示符命令

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

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