Solved

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

Posted on 2016-08-03
2
74 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:Tom Knowlton
[X]
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
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:Tom Knowlton
ID: 41751001
Thanks - sorry for the delay.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Upgrade code from VS 2010 to VS 2015 7 33
I need help setting focus to an @Html.TextBoxFor in my MVC.Net application 9 15
Json and ajax 1 22
asp.net web app 3 23
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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