201210182055C# - 取得執行檔(EXE)的回傳值

這段時間在做一些測試,主要是透過 A執行檔(EXE) 或者 A批次檔(BAT) 去呼叫一個 B執行檔並傳入相關參數後,B執行檔匯回傳一個數值,讓 A執行檔(EXE) 或者 A批次檔(BAT) 做之後的判斷。

這個議題說難不難,只能說江湖一點訣而已,以下就以範例的方式來呈現吧...

ConsoleApplication2:主要是讓 另外一個執行檔 或 批次檔 呼叫用,並且將傳進來的值回傳回去

Code:

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length > 0)
                Environment.Exit(int.Parse(args[0]));
            else
                Environment.Exit(0);
        }

        //二擇一使用

        //static int Main(string[] args)
        //{
        //    if (args.Length > 0)
        //        return Convert.ToInt32(args[0]);
        //    else
        //        return 0;
        //}
    }
}

ConsoleApplication1:呼叫 ConsoleApplication2

Code:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string strExitCode = string.Empty;

            ExecuteShellCommand(
                @"C:\Users\Administrator\Desktop\TESTFILE\ConsoleApplication1\ConsoleApplication2\bin\Debug\ConsoleApplication2.exe",
                "8989889",
                ref strExitCode
                );

            Console.WriteLine("Exit Code:{0}", strExitCode);
            Console.ReadKey();
        }

        public static void ExecuteShellCommand(string _FileToExecute, string _CommandLine, ref string _exitCode)
        {
            System.Diagnostics.Process P = null;
            try
            {
                P = new System.Diagnostics.Process();

                string strCmdProc = string.Format(System.Globalization.CultureInfo.InvariantCulture, @"{0}\cmd.exe", new object[] { Environment.SystemDirectory });
                string strArgs = string.Format(System.Globalization.CultureInfo.InvariantCulture, "/C {0}", new object[] { _FileToExecute });

                if (_CommandLine != null && _CommandLine.Length > 0)
                    strArgs += string.Format(System.Globalization.CultureInfo.InvariantCulture, " {0}", new object[] { _CommandLine, System.Globalization.CultureInfo.InvariantCulture });

                System.Diagnostics.ProcessStartInfo PI = new System.Diagnostics.ProcessStartInfo(strCmdProc, strArgs);
                PI.CreateNoWindow = true;
                PI.UseShellExecute = false;
                PI.RedirectStandardOutput = true;
                PI.RedirectStandardInput = true;
                PI.RedirectStandardError = true;
                P.StartInfo = PI;

                P.Start();

                P.WaitForExit();
                _exitCode = P.ExitCode.ToString();
            }
            catch (System.ComponentModel.Win32Exception Ex32)
            {
                Console.WriteLine("Win32 Exception:{0}", Ex32.ToString());
            }
            catch (Exception Ex)
            {
                Console.WriteLine("Exception:{0}", Ex.ToString());
            }
            finally
            {
                P.Close();
                P.Dispose();
                P = null;
            }
        }
    }
}

test.bat:呼叫 ConsoleApplication2,借用 MSDN 的範例

Code:

rem test.bat
@echo off
C:\Users\Administrator\Desktop\TESTFILE\ConsoleApplication1\ConsoleApplication2\bin\Debug\ConsoleApplication2.exe 8989889
@if "%ERRORLEVEL%" == "0" goto good

:fail
    echo Execution Failed
    echo return value = %ERRORLEVEL%
    goto end

:good
    echo Execution Succeded
    echo return value = %ERRORLEVEL%
    goto end

:end

PAUSE

結果一:ConsoleApplication1 呼叫 ConsoleApplication2


結果二:test.bat 呼叫 ConsoleApplication2


參考:
Main() Return Values (C# Programming Guide)
Environment.Exit 方法

沒有上一則|日誌首頁|沒有下一則
回應
關鍵字
累積 | 今日
loading......