Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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