protected override void Run() { Exception failure = null; bool error = false; try { Process p = new Process(); p.StartInfo.UseShellExecute = true; p.StartInfo.FileName = m_path; p.Start(); } catch (Win32Exception ex) { // If we catch the following Win32 error : // 1155 - No application is associated with the specified file for this operation // We fallback by showing the Open as... dialog. if (ex.NativeErrorCode == 1155) { Syscalls.SHELLEXECUTEINFO info = new Syscalls.SHELLEXECUTEINFO(); info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info); info.lpDirectory = Path.GetDirectoryName(m_path); info.lpFile = Path.GetFileName(m_path); info.nShow = (int)Syscalls.SW.SW_SHOWDEFAULT; info.lpVerb = "openas"; info.fMask = Syscalls.SEE_MASK.ASYNCOK; error = !Syscalls.ShellExecuteEx(ref info); if (error) failure = new Exception(Syscalls.GetLastErrorStringMessage()); } // Can't do anything with the problem, bail out. else failure = ex; } if (failure != null) { Logging.LogException(failure); MessageBox.Show("Unable to open " + m_path + Environment.NewLine + failure.Message, "Error on file open", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Open a given file using the Windows Shell. /// </summary> /// <param name="fullPath"></param> /// <returns>True on error, false otherwise.</returns> public static bool OpenFile(string fullPath, ref string errorMsg) { bool error = false; try { Logging.Log("OpenFile(" + fullPath + ") called."); Process p = new Process(); p.StartInfo.UseShellExecute = true; p.StartInfo.FileName = fullPath; p.Start(); Logging.Log("OpenFile: After p.Start()"); } catch (Win32Exception) { Logging.Log("OpenFile(): Caught Win32Exception"); // 1155 is the following error : // No application is associated with the specified file for this operation // Show the Open as... dialog in this case. if (Syscalls.GetLastError() == 1155) { Syscalls.SHELLEXECUTEINFO info = new Syscalls.SHELLEXECUTEINFO(); info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info); info.lpDirectory = Path.GetDirectoryName(fullPath); info.lpFile = Path.GetFileName(fullPath); info.nShow = (int)Syscalls.SW.SW_SHOWDEFAULT; info.lpVerb = "openas"; info.fMask = Syscalls.SEE_MASK.ASYNCOK; error = !Syscalls.ShellExecuteEx(ref info); } else { errorMsg = Syscalls.GetLastErrorStringMessage(); error = true; } } catch (Exception ex) { errorMsg = ex.Message; error = true; } Logging.Log("OpenFile exiting."); return error; }