解决使用C#打开第三方应用后进程关联问题
问题描述 :
打开开进程后调用第三方应用 , 关闭主应用时第三方应用也会关闭
(我这里要求第三方应用不能被关闭)
代码如下:
/// <summary> /// 打开文件选择框获取路径 /// eg: /// Cef.OpenApplication("**.exe"); /// </summary> /// <returns></returns> public void OpenApplication(string appName) { string dirpath = Application.StartupPath; if (!File.Exists(dirpath+"\\"+appName)) { MessageBox.Show("启动失败,文件不存在"); return; } //System.Diagnostics.Process.Start(dirpath + "\\" + appName); ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder(appName), new StringBuilder(""), new StringBuilder(dirpath), 1); } /// <summary> /// 打开第三方应用 /// 示例 : /// ShellExecute(IntPtr.Zero, new StringBuilder("Open"), new StringBuilder("test.exe"), new StringBuilder(""), new StringBuilder(@"C:\文件夹名"), 1); /// </summary> /// <param name="hwnd"></param> /// <param name="lpszOp"></param> /// <param name="lpszFile"></param> /// <param name="lpszParams"></param> /// <param name="lpszDir"></param> /// <param name="FsShowCmd"></param> /// <returns></returns> [DllImport("shell32.dll")] public static extern int ShellExecute(IntPtr hwnd, StringBuilder lpszOp, StringBuilder lpszFile, StringBuilder lpszParams, StringBuilder lpszDir, int FsShowCmd);
查看进程时发现进程被关联了
解决方案 :
使用资源管理器打开第三方应用 , 就像手动执行的结果一样
代码如下 :
/// <summary> /// 打开文件选择框获取路径 /// eg: /// Cef.OpenApplication("**.exe"); /// </summary> /// <returns></returns> public void OpenApplication(string appName) { string dirpath = Application.StartupPath; if (!File.Exists(dirpath+"\\"+appName)) { MessageBox.Show("启动失败,文件不存在"); return; } System.Diagnostics.Process.Start("Explorer.exe", dirpath + "\\" + appName);
//ShellExecute(IntPtr.Zero, new StringBuilder("Open"), // new StringBuilder("Explorer"), new StringBuilder(dirpath + "\\" + appName), new StringBuilder(""), 1);//好使,会多打开多一个explorer.exe //System.Diagnostics.Process.Start(dirpath + "\\" + appName); //ShellExecute(IntPtr.Zero, new StringBuilder("Open"), // new StringBuilder(appName), new StringBuilder(""), new StringBuilder(dirpath), 1); }
打开程序时有可能会弹出杀毒软件阻止窗口 , 如下
附 :
使用 Process.Start("Explorer.exe", dirpath + "\\" + appName);打开应用后附进程不存在
使用
ShellExecute(IntPtr.Zero, new StringBuilder("Open"),
new StringBuilder("Explorer"), new StringBuilder(dirpath + "\\" + appName), new StringBuilder(""), 1);
打开应用后 , 任务管理器中会出现两个 explorer.exe 进程