2

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.

Mark
  • 3,609
  • 1
  • 22
  • 33
Assassino
  • 31
  • 1
  • 3
  • 11
  • 1
    Executing `cmd /c d:` changes the working directory to D: *for that instance of cmd.exe.* But then that instance exits and the next time you run cmd.exe, the directory will be the same as the current working directory of your program. – Jim Mischel Mar 16 '14 at 18:27
  • 1
    Oh, and by the way: there's no such thing as a "DOS prompt" unless you're running Windows 98. It's the Windows Command Prompt. DOS hasn't existed in Windows for 15 years. – Jim Mischel Mar 16 '14 at 18:29

2 Answers2

1
  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 runascommand. 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.

shashwat12
  • 168
  • 8
  • runas didnt work when useing shell option is set to true. is there other way to add the installer to a registery – Assassino Mar 17 '14 at 18:05
  • Please clear to me that whether you want your cmd commands to run with admin privileges or you want your c# program to run with admin privileges when installed. – shashwat12 Mar 18 '14 at 05:32
  • definitely not my cmd, i want the c# program to run as administrator – Assassino Apr 06 '14 at 15:31
0

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

Community
  • 1
  • 1
AlexMelw
  • 2,406
  • 26
  • 35