Click here to Skip to main content
15,902,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I am trying to make a bash-like shell in C#,
Well, for executing cmd commands I am starting the cmd.exe, hidding it, and displaying the output in my Console Application using this code :
C#
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("Microsoft(R) Windows(R) Command Prompt");
Console.ForegroundColor = ConsoleColor.White;
Console.Write("@bash!~cmd: ");
string cmdimput = Console.ReadLine();
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.StartInfo.Arguments = cmdimput;
cmd.StartInfo.RedirectStandardError = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.ErrorDialog = false;
cmd.Start();
StreamWriter inputWriter = cmd.StandardInput;
StreamReader outputReader = cmd.StandardOutput;
StreamReader errorReader = cmd.StandardError;
cmd.WaitForExit();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
Console.ReadLine();

The problem is that my Application halts as soon as I press enter after entering an argument,
No Output/Input and error messages are found, It just hangs....

Help Would be appreciated...
@bash!cmd~: Sids123 :P
Posted

1 solution

Since you start cmd.exe directly, it will start and wait for the user to close the window. But since you have hidden it, the user won't know that he has to close it, or how to close it if he guesses that he should.

You then say WaitForExit - so your application blocks waiting for the user to close a window he can't see, can't touch, and doesn't know is there...

And this surprises you?

Try removing the Hidden style from your window, and you'll see what I mean. Oh, and if you have tested this several times, you might want to use the Task Manager to shut down those hidden windows...

I'm not exactly sure what you are trying to achieve with this, but that isn't the way to do it.
 
Share this answer
 
Comments
sid2x 4-Sep-13 7:11am    
Thanks for the solution but all in vain, I couldn't get that to work :(
OriginalGriff 4-Sep-13 7:21am    
It's not a solution - it just points out what you have done wrong...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900