0

I need to write a small utility to rebuild solution. I am using below code to do the same.

        string solutionFile = @"E:\Projects\TFS\Code\WebSite.sln";
        string cmd1 = @"""C:\Program Files\Microsoft Visual Studio 11.0\VC\vcvarsall.bat"" x86" + " &devenv " + "\"" + solutionFile + "\"" + " /rebuild release";
        cmd1 = "\"" + cmd1 + "\"";
        String command = String.Format("{0} {1}", @"/k ", cmd1);
        ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe")
        {
            UseShellExecute = false,
            RedirectStandardOutput = true
        };

        cmdsi.Arguments = command;
        using (Process cmd = Process.Start(cmdsi))
        {
            using (StreamReader reader = cmd.StandardOutput)
            {
                string result = reader.ReadToEnd();
                listBox1.Items.Add(result);
            }
        }

If you will observe in command prompt then you can see executions output but same thing is not getting reflected in list box.

Please help to solve this issue.

Thank you in advance.

Vikesh
  • 3
  • 3
  • I recall having done this before, however I don't recall ever encountering any problem like this. That said, have you tried invoking the "listBox1.Items.Add" on the main thread? It might be a thread-related problem. – Falgantil Jan 28 '15 at 10:16
  • Because of main thread it won't show result immediately, but after completion of execution it shall show output. But it's showing only one space (" "). I have refured this [link](http://www.dotnetperls.com/redirectstandardoutput) – Vikesh Jan 28 '15 at 10:21
  • Try simplifying `cmd1` so it just runs a basic batch file that echoes a line of text to the console, and see if that works. That should narrow down the problem to either the code you have shown or the batch file that you are calling. – Steven Rands Jan 28 '15 at 10:38
  • If you see output on the console window then you *know* it didn't get redirected. You are not redirecting StandardError, that's one mistake. Best way to do this is to let cmd.exe take care of the redirection completely, redirecting to a file. Avoids the icky deadlock problems. Sample code [is here](http://stackoverflow.com/a/20789248/17034). – Hans Passant Jan 28 '15 at 12:58

2 Answers2

0

You can redirect the output to a temporary file and then can read the file like-

string cmd1 = "help > e:/temp.txt"; //e:/temp.txt is temporary file where the output is redirected.
        String command = String.Format("{0} {1}", @"/k ", cmd1);

        ProcessStartInfo cmdsi = new ProcessStartInfo("cmd.exe")
        {
            //You don't need to read console outputstream
            //UseShellExecute = false,
            //RedirectStandardOutput = true
        };

        cmdsi.Arguments = command;
        using (Process cmd = Process.Start(cmdsi))
        {
            //Check if file exist or you can wait till the solution builds completely. you can apply your logic to wait here.
            if (File.Exists("E:/temp.txt"))
            {
                //Read the files here 
                string[] lines = File.ReadAllLines("E:/temp.txt");
                //Do your work here
            }
        }
Rohit Prakash
  • 1,975
  • 1
  • 14
  • 24
0

You can do it async:

string solutionFile = @"E:\Projects\TFS\Code\WebSite.sln";
string batFile = @"C:\Program Files\Microsoft Visual Studio 11.0\VC\vcvarsall.bat";
string args = "x86" + " &devenv " + "\"" + solutionFile + "\"" + " /rebuild release";

ProcessStartInfo cmdsi = new ProcessStartInfo(batFile)
{
                Arguments = args,
                UseShellExecute = false,
                RedirectStandardOutput = true
};

using (Process cmd = new Process())
{
            cmd.StartInfo = cmdsi;
            cmd.OutputDataReceived += (sender, args) => listBox1.Items.Add(string.IsNullOrEmpty(args.Data) ? string.Empty : args.Data);
            cmd.Start();
}
mdghost
  • 376
  • 3
  • 8