سؤال

I have an .exe file which was written in C. It is a command line application. I want give command line and also get correspond output in this application through a C# application.

How do I invoke the command and get the output from C#?

هل كانت مفيدة؟

المحلول

You could use the Process.Start method:

class Program
{
    static void Main()
    {
        var psi = new ProcessStartInfo
        {
            FileName = @"c:\work\test.exe",
            Arguments = @"param1 param2",
            UseShellExecute = false,
            RedirectStandardOutput = true,
        };
        var process = Process.Start(psi);
        if (process.WaitForExit((int)TimeSpan.FromSeconds(10).TotalMilliseconds))
        {
            var result = process.StandardOutput.ReadToEnd();
            Console.WriteLine(result);
        }
    }
}

نصائح أخرى

You need to use the Process.Start method.

You supply it with the name of your process and any command line arguments and it will run the executable.

You can capture any output which you can then process in your C# application.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top