Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2016-08-03
2
Medium Priority
?
103 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 2000 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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