最近的任务用到C#来调用C++内核程序,也就是C++编译运行后生成的.exe文件。网搜了一下C#中运行外部程序大致有两种方法,在此稍作总结:
(1)使用API函数ShellExcute
添加引用 using System.Runtime.InteropServices;
public enum ShowWindowCommands : int
{
SW_HIDE = 0,
SW_SHOWNORMAL = 1, //用最近的大小和位置显示,激活
SW_NORMAL = 1,
SW_SHOWMINIMIZED = 2,
SW_SHOWMAXIMIZED = 3,
SW_MAXIMIZE = 3,
SW_SHOWNOACTIVATE = 4,
SW_SHOW = 5,
SW_MINIMIZE = 6,
SW_SHOWMINNOACTIVE = 7,
SW_SHOWNA = 8,
SW_RESTORE = 9,
SW_SHOWDEFAULT = 10,
SW_MAX = 10
}
[DllImport("shell32.dll")]
public static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpszOp,
string lpszFile,
string lpszParams,
string lpszDir,
ShowWindowCommands FsShowCmd
);
执行语句只有这句:
ShellExecute(IntPtr.Zero, "open", "testIO.exe", null, null, ShowWindowCommands.SW_SHOWNORMAL);
对于ShellExecute函数,http://baike.baidu.com/link?url=j6AhrfaS5YyMQ_8peNIdCsM0SWCjMoVDK_Lbzi5A4lz7VjNNCRsZU-bXrYFgfe6T_Rd1MKjQ9GelkuolNqNx1K
(2)使用Process类
添加引用using System.Diagnostics;
Process process = new Process();
//process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "testIO.exe";
//process.StartInfo.CreateNoWindow = true;
process.Start();
如此即可用C#打开testIO.exe文件。。。。。。
C#如何运行外部程序(打开可执行程序):ShellExcute和Process,布布扣,bubuko.com
C#如何运行外部程序(打开可执行程序):ShellExcute和Process
原文:http://blog.csdn.net/wrm_nancy/article/details/20299725