프로그램 자료/Visual C#

콘솔 프로그램 실행 후 결과값 받기 command result

motolies 2016. 11. 14. 13:37

출처 : 나





public static bool ExecuteCommand(string exeFilePath, string arguments, out string result)

{

    ProcessStartInfo processStartInfo = new ProcessStartInfo();

    processStartInfo.FileName = exeFilePath;

    processStartInfo.Arguments = arguments;

    processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

    processStartInfo.CreateNoWindow = true;

    processStartInfo.RedirectStandardOutput = true;

    processStartInfo.UseShellExecute = false;

 

    Process p = new Process();

    p.StartInfo = processStartInfo;

 

    try

    {

        p.Start();

        result = p.StandardOutput.ReadToEnd();

        return true;

    }

    catch (Exception x)

    {

        result = x.Message;

        return false;

    }

    finally

    {

        //Wait for the process to end.

        p.WaitForExit();

        p.Close();

    }

}