Pergunta

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! :)

Foi útil?

Solução

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();

Outras dicas

First off you seem to kill it staright away after starting it, so unless it can do what its got to do in like nanoseconds it will never output anything

You're killing the Process right after you start it.

If the Process exits on its own, you can do the following:

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

If you would only like to give it so much time to execute:

Compare.WaitForExit(5000); //Wait 5 seconds.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top