Search code examples
c#processconsole-applicationsystem.diagnostics

Something with/in Start.Process() is not working


My console application has a void in it which opens an external console application, to compare 2 text files.

I get no errors, so I assume it's working. But when I look at the output, I see NOTHING. When I open the application that compares the text files, it's working perfectly. So I think there must be something wrong with the void.

Here's my code. I used a combination of examples from MSDN as well as stackoverflow and other websites. But nothing so far. Maybe it's really obvious and I'm just stupid haha

using System.IO;
using System.Security.Permissions;
using System.Diagnostics;

static void Compare()
    {
        Process Compare = new Process();

        try
        {
            Compare.StartInfo.UseShellExecute = false;
            Compare.StartInfo.FileName = @"C:\Path\To\The\File.exe";
            Compare.StartInfo.CreateNoWindow = true;
            Compare.Start();
            Compare.Kill();
        }

        catch (Exception)
        {
            Compare.Kill();
        }
    }

If anyone can tell me what is wrong with it, I would appreciate it! :)


Solution

  • You are killing it right after starting it

    Compare.Start();
    Compare.Kill();
    

    Remove the Compare.Kill(); line and try again.

    In addition, if you want to receive detailed output from the started process you will have to use async events:

     Process process = new Process();
     process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);     
     process.Exited += new EventHandler(process_Exited);
     process.ErrorDataReceived += new DataReceivedEventHandler(process_ErrorDataReceived); 
     process.Start();    
     process.BeginOutputReadLine();
     process.BeginErrorReadLine();    
     process.WaitForExit();