0

I need to run cmd commands in a c# program im writing, the only solutions ive found make a new command line instance for every command, but i need them to be executed in the same instance

Run Command Prompt Commands

public static void UpdateDeviceData()
        {
            Process.Start("CMD.exe",
                String.Format("client_commandline.exe setdeviceposition 0 {0} {1} {2}", LPos.X, LPos.Y, LPos.Z));
        }
OmegaRogue
  • 11
  • 1
  • 5
  • `but i need them to be executed in the same instance`; may I ask why? – Stefan Mar 24 '19 at 16:57
  • Note that it is not *needed* to use CMD.EXE to start other executable. You can start the other executable(s) directly, with their proper parameters. Doing so will be a bit more efficient, also it will not create a visible cmd.exe window (could it be that this is what you are aiming to solve with this?). – Peter B Mar 24 '19 at 17:31
  • i found a solution, its this: ```csharp Process CmdPro; ProcessStartInfo startInfo; startInfo = new ProcessStartInfo(); startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = false; startInfo.RedirectStandardError = false; startInfo.FileName = "CMD.exe"; CmdPro = new Process(); CmdPro.StartInfo = startInfo; CmdPro.Start(); Console.SetOut(CmdPro.StandardInput); ``` – OmegaRogue Mar 24 '19 at 19:25
  • @Stefan i thought it needed to be in the same instance but apparently it doesnt need to be – OmegaRogue Mar 24 '19 at 19:29

1 Answers1

0

i found a solution, its this:

Process CmdPro;
ProcessStartInfo startInfo; 
startInfo = new ProcessStartInfo(); 
startInfo.UseShellExecute = false; 
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = false; 
startInfo.RedirectStandardError = false; 
startInfo.FileName = "CMD.exe"; 
CmdPro = new Process(); 
CmdPro.StartInfo = startInfo; CmdPro.Start(); 
Console.SetOut(CmdPro.StandardInput);
OmegaRogue
  • 11
  • 1
  • 5