明飞的技术园地

笨鸟先飞
  博客园  :: 新随笔  :: 联系 :: 管理

Process.Start 在xp系统中因权限导致执行失败的问题

Posted on 2010-04-24 20:29  明飞  阅读(650)  评论(0编辑  收藏  举报

在做软件维护时遇到一个问题:在某个用户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);