CMD command not running in console - c#

I have some code that runs a cmd command in C# which works really well in a WinForm but when running this in a console app it doesn't work. I am a bit stuck as to why this is, I tried adding Windows.Forms as a reference and added the using to the code but this didn't work either. The only other thing I can think of is that because it is running as a console it can't run another console window on top of this?
Any help is appreciated.
ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.RedirectStandardOutput = true;
cmd.RedirectStandardError = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = true;
cmd.WindowStyle = ProcessWindowStyle.Hidden;
Process console = Process.Start(cmd);
console.StandardInput.WriteLine("command to run");

The following code will perform any console command you want and output the console text in your current window, everything after while(true) is just as example:
ProcessStartInfo cmd = new ProcessStartInfo("cmd.exe");
cmd.RedirectStandardInput = true;
cmd.UseShellExecute = false;
cmd.CreateNoWindow = false;
cmd.WindowStyle = ProcessWindowStyle.Normal;
Process console = Process.Start(cmd);
while(true)
console.StandardInput.WriteLine("pause");
If you don't want any console output then set CreateNoWindow to true. Also this code works inside a console application using System.Diagnostics
Good luck!

Related

c# vsto calling exe , works only half of the times

Hello everyone and thank you for the help
I made a word Addin while clicking some of the buttons like upload file or login,c# calling exe to run
But for some reason, the exe does not work each time I try, only half of the time, and if I succeed, when I am trying to call the exe again for a different purpose, I failed (nothing happend)
one more thing, I need to get the output of the exe file.
the exe in the debug folder
the code below:
ProcessStartInfo processInfo = new ProcessStartInfo();
processInfo.WorkingDirectory = Path.GetDirectoryName("Api_Layer.exe");
processInfo.FileName = "Api_Layer.exe";
processInfo.ErrorDialog = true;
processInfo.UseShellExecute = false;
processInfo.RedirectStandardOutput = true;
processInfo.RedirectStandardError = true;
Process proc = Process.Start(processInfo);
List<string> output = new List<string>();
while (!proc.HasExited)
{
output.Add(proc.StandardOutput.ReadToEnd());
}

C# How can I pop up Command Prompt(CMD) in running Console?

In c#, run new process cmd and send out command(ex.. ping 127.0.0.1 -t) to cmd
Then redirect Standardoutput to Console..
But in compile I cannot see cmd which is working in pop up window, only know that command is working on hidden state.
How can I show up command prompt like console pop up?? I want to see console window and command prompt at the same time..
If I don't redirect from cmd and only send the command(ex dir) to cmd,
still command prompt window does't showed up and only see console window..
Process proc_cmd = new Process();
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = "cmd.exe";
startinfo.WorkingDirectory = #"c:\";
startinfo.UseShellExecute = false;//to use RedirectStandard~
startinfo.RedirectStandardInput = true;
startinfo.RedirectStandardOutput = true;
startinfo.RedirectStandardError = true;
proc_cmd.EnableRaisingEvents = false;
proc_cmd.StartInfo = startinfo;
proc_cmd.Start();
proc_cmd.StandardInput.Write(#"ping 127.0.0.1 -t" +
Environment.NewLine);
proc_cmd.StandardInput.Close();
type start in command prompt:
start
This will open up a new cmd window.
You may need something like this if you need to view the console on debug:
startInfo.CreateNoWindow = false;

How to have single command window for multiple processes in c#

I have a winforms application where I run a process (code shown below) in a loop. Suppose I have 10 items, I get 10 command windows where the actions are run. So, I wanted to know if there is a way where I can run the process 10 times but have just one command window open where all my actions are run.
Process p = new Process();
try
{
var pInfo = new ProcessStartInfo(executable, args);
pInfo.UseShellExecute = false;
pInfo.RedirectStandardOutput = true;
pInfo.RedirectStandardError = true;
pInfo.WorkingDirectory = workingDir;
p.StartInfo = pInfo;
p.EnableRaisingEvents = true;
SubscribeEvents(p);
p.Start();
p.WaitForExit();
}
I guess if you are doing winforms, you actually already probably have a main window open.
You can use this to hide your other windows:
pInfo.CreateNoWindow = true;

Child process not getting created when called from winform button event function?

I am trying to run a python code from my UI. i am using the code below to do so,
Process p = new Process();
string cmd = #"python filepath & exit";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.RedirectStandardInput = true;
p.Start();
StreamWriter myStreamWriter = p.StandardInput;
myStreamWriter.WriteLine(cmd.ToString());
myStreamWriter.Close();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
Console.ReadLine();
the above code opens a cmd prompt and executes the python file.
this code runs fine when i test it using a console application but it doesn't work when i use it within the event function for "run" button in my UI application. Is there any particular reason for it?

C#: Run external console program as hidden

can anyone tell me how to spawn another console application from a Winforms app, but (A) not show the console window on the screen, and (B) still obtain the standard output of the application? Currently I have something like the following:
Process SomeProgram = new Process();
SomeProgram.StartInfo.FileName = #"c:\foo.exe";
SomeProgram.StartInfo.Arguments = "bar";
SomeProgram.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
SomeProgram.StartInfo.UseShellExecute = false;
SomeProgram.StartInfo.RedirectStandardOutput = true;
SomeProgram.Start();
SomeProgram.WaitForExit();
string SomeProgramOutput = SomeProgram.StandardOutput.ReadToEnd();
If I set RedirectStandardOutput to false, then the console app is hidden as expected, but I cannot get the standard output text. However, as soon as I set the RedirectStandardOutput to true, the window stops being hidden, although I am able to get the program's output.
So, I know how to make the console app run hidden, and I know how to get the program's output, but how do I get it to do both?
Many TIA
You are missing the CreateNoWindow property which has to be set to true in your case.
I think it will help you:
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
pProcess.StartInfo.FileName = #"C:\Users\Vitor\ConsoleApplication1.exe";
pProcess.StartInfo.Arguments = "olaa"; //argument
pProcess.StartInfo.UseShellExecute = false;
pProcess.StartInfo.RedirectStandardOutput = true;
pProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
pProcess.StartInfo.CreateNoWindow = true; //not diplay a windows
pProcess.Start();
string output = pProcess.StandardOutput.ReadToEnd(); //The output result
pProcess.WaitForExit();

Categories

Resources