简体   繁体   中英

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#. I have done it in one way by setting the StandardOutput... and input to 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.

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.

  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. 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.
  3. If you want to execute command using administrator rights you can use runas command. It is equivalent to the sudo command in Linux . 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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