在做软件维护时遇到一个问题:在某个用户xp系统中Process.Start启动外部程序时导致报错,使用administrator账户运行才能正常运行,后来经过调试采用api函数WinExec和ShellExecute,无论什么用户都能运行正常。其中ShellExecute功能更强除了执行exe文件还可以执行等其它只要在系统中注册关联的程序,如word,excel文件等。
[DllImport("kernel32.dll")]
public static extern uint WinExec(string lpCmdLine,uint uCmdShow);
public const int SW_SHOW=5;
[DllImport("SHELL32.dll")]
public static extern int ShellExecute(int hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
int nShowCmd
);
public const int SW_SHOWNORMAL=11;
执行时写法
WinExec(fileName,SW_SHOW);
ShellExecute(0,"open",fileName,null,null,SW_SHOWNORMAL);