寫了一段程式實現對另一個程式打包調用,同時可以加入一些延遲之類的操作,需要注意的是匯入所有被調程式需要的參數,匯出所有其返回的值。
--------------------
CallProgram
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Diagnostics;
5 using System.Threading;
6
7 public static int StartProcess(string filename, string[] args)
8 {
9 // Create a process for the EXE file you want to call.
10 Process myProcess = new Process();
11 int res = -1;
12 try
13 {
14 string sArg = "";
15 foreach (string arg in args)
16 {
17 sArg = sArg + arg + " ";
18 }
19 sArg = sArg.Trim();
20 // The ProcessStartInfo that represents the data with which to start the process. These arguments include the name of the executable file or document used to start the process.
21 ProcessStartInfo startInfo = new ProcessStartInfo(filename, sArg);
22 myProcess.StartInfo = startInfo;
23 // True to use the shell when starting the process; otherwise, the process is created directly from the executable file. The default is true.
24 myProcess.StartInfo.UseShellExecute = false;
25 myProcess.Start();
26 myProcess.WaitForExit(); // Remember to wait for exit here.
27 res = myProcess.ExitCode; // Return the ExitCode from the EXE file you called.
28 }
29 catch (Exception ex)
30 {
31 Console.Out.WriteLine(ex.Message);
32 }
33 finally
34 {
35 if (myProcess != null)
36 myProcess.Close();
37 }
38 return res;
39 }
40
41 static int Main(string[] args)
42 {
43 // Full name of the EXE file to be called.
44 string strFileName = "c:\\test.exe";
45
46 // If want to add delay, sleep could be used here.
47
48 Thread.Sleep(60000);
49 // Get args from Main.
50 int res = StartProcess(strFileName, args);
51 // Return the value what EXE file returns.
52 return res;
53 }
參考:http://msdn.microsoft.com/en-us/library/system.diagnostics.process(VS.71).aspx