C#实现对程序打包调用
时间:2010-10-21 来源:Pegasus923

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Threading;
public static int StartProcess(string filename, string[] args)
{
// Create a process for the EXE file you want to call.
Process myProcess = new Process();
int res = -1;
try
{
string sArg = "";
foreach (string arg in args)
{
sArg = sArg + arg + " ";
}
sArg = sArg.Trim();
// 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.
ProcessStartInfo startInfo = new ProcessStartInfo(filename, sArg);
myProcess.StartInfo = startInfo;
// True to use the shell when starting the process; otherwise, the process is created directly from the executable file. The default is true.
myProcess.StartInfo.UseShellExecute = false;
myProcess.Start();
myProcess.WaitForExit(); // Remember to wait for exit here.
res = myProcess.ExitCode; // Return the ExitCode from the EXE file you called.
}
catch (Exception ex)
{
Console.Out.WriteLine(ex.Message);
}
finally
{
if (myProcess != null)
myProcess.Close();
}
return res;
}
static int Main(string[] args)
{
// Full name of the EXE file to be called.
string strFileName = "c:\\test.exe";
// If want to add delay, sleep could be used here.
Thread.Sleep(60000);
// Get args from Main.
int res = StartProcess(strFileName, args);
// Return the value what EXE file returns.
return res;
}
参考:http://msdn.microsoft.com/en-us/library/system.diagnostics.process(VS.71).aspx
相关阅读 更多 +