Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

After executing a command in c#, the command is not closing automatically and also I tried by passing a exit command, but no change.

When I am closing it manually the error message is displaying as "^C"

But the command is working to the requirement but cmd is not closing.


Can any one help what might be the problem.

What I have tried:

C#
ProcessStartInfo start = new ProcessStartInfo();
                start.FileName = @"C:\WINDOWS\system32\cmd.exe";
                start.UseShellExecute = false;
                start.RedirectStandardOutput = true;
                start.RedirectStandardInput = true;
                start.RedirectStandardError = true;
                start.CreateNoWindow = false;
                Process proc1 = new Process();
                proc1.StartInfo = start;
                proc1.Start();
                using (StreamWriter sw = proc1.StandardInput)
                {
                    if (sw.BaseStream.CanWrite)
                    {
                        sw.WriteLine("cd " + "Specified folder");
                        sw.WriteLine(mycommand);
                        sw.WriteLine("exit");
                    }
                }
                    proc1.WaitForExit();
                string outputval = proc1.StandardError.ReadToEnd();
                if (!string.IsNullOrEmpty(outputval))
                    MessageBox.Show(outputval);
Posted
Updated 4-Feb-18 22:21pm
v2

1 solution

I tried your code, with one minor change, and it works successfully. My change:
C#
proc1.WaitForExit();
string outputval = proc1.StandardOutput.ReadToEnd(); // read output of command, not error


[edit]
When I read StandardError it is blank. The problem must be in the command that you send to the process.
[/edit]
 
Share this answer
 
v2
Comments
Sudheer gadde 5-Feb-18 6:27am    
Thanks for your time and saying that no error in process but only with my command....I will check once and come back to you..

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