Solved

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

Posted on 2016-08-03
2
60 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 40

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 41741406
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
ID: 41751001
Thanks - sorry for the delay.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

863 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

20 Experts available now in Live!

Get 1:1 Help Now