Solved

Can run one command via cmd.exe from .NET Windows Form - want to run several

Posted on 2016-08-03
2
52 Views
Last Modified: 2016-08-10
This method below allows me to run ONE dos command in a command prompt and output the result to a textbox.

That is all fine and good, but I don't want to have to exit.  I want to be able to send multiple commands in one session, not one at a time, but still using .NET as I am now.

If I don't run the "exit" command - the proc.ReadStandardOutput.ReadToEnd(  ) stays in a "waiting" state and I never get to see the result of the command in the textBox window.

[DllImport("user32.dll")]
        static extern bool SetForegroundWindow(IntPtr hWnd);

        private void RunGitCommandLine(string command)
        {
            ProcessStartInfo psi = new ProcessStartInfo("cmd");
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.CreateNoWindow = true;
            psi.RedirectStandardInput = true;
            var proc = Process.Start(psi);

            proc.StandardInput.WriteLine(@command);
            proc.StandardInput.WriteLine("exit");            
            string s = proc.StandardOutput.ReadToEnd();

            textBoxCommandResult.Text = s;
        }

Open in new window

0
Comment
Question by:knowlton
2 Comments
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
Comment Utility
You can output the log to a text file . . . or multiple text files.

Essentially:

string g = Guid.NewGuid().ToString() +".txt";

//override to append to text file.
  proc.StandardInput.WriteLine(@command + " >> " + g);

Open in new window


Then you can either wait for the file to exist or append multiple outputs to the same file and read it in all at once.

If you're going to wait you might want to spawn this off in another thread.  You can create one huge text files or one text file per command and then combine it on the .net side.
0
 
LVL 5

Author Closing Comment

by:knowlton
Comment Utility
Thanks - sorry for the delay.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now