C#实现对程序打包调用

写了一段程序实现对另一个程序打包调用,同时可以加入一些延迟之类的操作,需要注意的是导入所有被调程序需要的参数,导出所有其返回的值。

--------------------

 

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

 
posted on 2010-10-21 17:50  浩然119  阅读(260)  评论(0编辑  收藏  举报