public static void Restart() { try { Process p = Process.Start(NativeLib.EncodePath(WinUtil.GetExecutable())); if (p != null) { p.Dispose(); } } catch (Exception ex) { MessageService.ShowWarning(ex); } }
internal static void OpenUrlDirectly(string strUrl) { if (string.IsNullOrEmpty(strUrl)) { Debug.Assert(false); return; } string str = NativeLib.EncodePath(strUrl); try { Process p = Process.Start(str); if (p != null) { p.Dispose(); } } catch (Exception ex) { MessageService.ShowWarning(str, ex); } }
public static bool RunElevated(string strExe, string strArgs, bool bShowMessageIfFailed) { if (strExe == null) { throw new ArgumentNullException("strExe"); } try { ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = NativeLib.EncodePath(strExe); if (!string.IsNullOrEmpty(strArgs)) { psi.Arguments = strArgs; } psi.UseShellExecute = true; // Elevate on Windows Vista and higher if (WinUtil.IsAtLeastWindowsVista) { psi.Verb = "runas"; } Process p = Process.Start(psi); if (p != null) { p.Dispose(); } } catch (Exception ex) { if (bShowMessageIfFailed) { MessageService.ShowWarning(ex); } return(false); } return(true); }
private static void OpenUrlPriv(string strUrlToOpen, PwEntry peDataSource, bool bAllowOverride, string strBaseRaw) { if (string.IsNullOrEmpty(strUrlToOpen)) { Debug.Assert(false); return; } if (WinUtil.OpenUrlPre != null) { OpenUrlEventArgs e = new OpenUrlEventArgs(strUrlToOpen, peDataSource, bAllowOverride, strBaseRaw); WinUtil.OpenUrlPre(null, e); strUrlToOpen = e.Url; if (string.IsNullOrEmpty(strUrlToOpen)) { return; } } string strPrevWorkDir = WinUtil.GetWorkingDirectory(); string strThisExe = WinUtil.GetExecutable(); string strExeDir = UrlUtil.GetFileDirectory(strThisExe, false, true); WinUtil.SetWorkingDirectory(strExeDir); string strUrl = CompileUrl(strUrlToOpen, peDataSource, bAllowOverride, strBaseRaw, null); Process p = null; if (WinUtil.IsCommandLineUrl(strUrl)) { string strApp, strArgs; StrUtil.SplitCommandLine(WinUtil.GetCommandLineFromUrl(strUrl), out strApp, out strArgs, true); try { if (!string.IsNullOrEmpty(strArgs)) { p = Process.Start(strApp, strArgs); } else { p = Process.Start(strApp); } } catch (Win32Exception) { StartWithoutShellExecute(strApp, strArgs); } catch (Exception exCmd) { string strMsg = KPRes.FileOrUrl + ": " + strApp; if (!string.IsNullOrEmpty(strArgs)) { strMsg += MessageService.NewParagraph + KPRes.Arguments + ": " + strArgs; } MessageService.ShowWarning(strMsg, exCmd); } } else // Standard URL { try { p = Process.Start(NativeLib.EncodePath(strUrl)); } catch (Exception exUrl) { MessageService.ShowWarning(strUrl, exUrl); } } try { if (p != null) { p.Dispose(); } } catch (Exception) { Debug.Assert(false); } // Restore previous working directory WinUtil.SetWorkingDirectory(strPrevWorkDir); // SprEngine.Compile might have modified the database MainForm mf = Program.MainForm; if (mf != null) { mf.UpdateUI(false, null, false, null, false, null, false); } }
private static byte[] OpenExternal(string strName, byte[] pbData, BinaryDataOpenOptions opt) { byte[] pbResult = null; try { string strBaseTempDir = UrlUtil.EnsureTerminatingSeparator( UrlUtil.GetTempPath(), false); string strTempID, strTempDir; while (true) { byte[] pbRandomID = CryptoRandom.Instance.GetRandomBytes(8); strTempID = Convert.ToBase64String(pbRandomID); strTempID = StrUtil.AlphaNumericOnly(strTempID); if (strTempID.Length == 0) { Debug.Assert(false); continue; } strTempDir = strBaseTempDir + strTempID; if (!Directory.Exists(strTempDir)) { Directory.CreateDirectory(strTempDir); strTempDir = UrlUtil.EnsureTerminatingSeparator( strTempDir, false); // Mark directory as encrypted, such that new files // are encrypted automatically; there exists no // Directory.Encrypt method, but we can use File.Encrypt, // because this internally uses the EncryptFile API // function, which works with directories try { File.Encrypt(strTempDir); } catch (Exception) { Debug.Assert(false); } break; } } strName = UrlUtil.GetSafeFileName(strName); string strFile = strTempDir + strName; File.WriteAllBytes(strFile, pbData); // Encrypt again, in case the directory encryption above failed try { File.Encrypt(strFile); } catch (Exception) { Debug.Assert(false); } ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = NativeLib.EncodePath(strFile); psi.UseShellExecute = true; psi.WorkingDirectory = strTempDir; ParameterizedThreadStart pts = new ParameterizedThreadStart( BinaryDataUtil.ShellOpenFn); Thread th = new Thread(pts); th.Start(psi); string strMsgMain = KPRes.AttachExtOpened + MessageService.NewParagraph + KPRes.AttachExtOpenedPost + ":"; string strMsgImp = KPRes.Import; string strMsgImpDesc = KPRes.AttachExtImportDesc; string strMsgCancel = KPRes.DiscardChangesCmd; string strMsgCancelDesc = KPRes.AttachExtDiscardDesc; VistaTaskDialog vtd = new VistaTaskDialog(); vtd.CommandLinks = true; vtd.Content = strMsgMain; vtd.MainInstruction = strName; vtd.WindowTitle = PwDefs.ShortProductName; vtd.SetIcon(VtdCustomIcon.Question); vtd.AddButton((int)DialogResult.OK, strMsgImp, strMsgImpDesc); vtd.AddButton((int)DialogResult.Cancel, strMsgCancel, strMsgCancelDesc); vtd.FooterText = KPRes.AttachExtSecDel; vtd.SetFooterIcon(VtdIcon.Information); bool bImport; if (vtd.ShowDialog()) { bImport = ((vtd.Result == (int)DialogResult.OK) || (vtd.Result == (int)DialogResult.Yes)); } else { StringBuilder sb = new StringBuilder(); sb.AppendLine(strName); sb.AppendLine(); sb.AppendLine(strMsgMain); sb.AppendLine(); sb.AppendLine("[" + KPRes.Yes + "]:"); sb.AppendLine(StrUtil.RemoveAccelerator(strMsgImp) + " - " + strMsgImpDesc); sb.AppendLine(); sb.AppendLine("[" + KPRes.No + "]:"); sb.AppendLine(StrUtil.RemoveAccelerator(strMsgCancel) + " - " + strMsgCancelDesc); sb.AppendLine(); sb.AppendLine(KPRes.AttachExtSecDel); bImport = MessageService.AskYesNo(sb.ToString()); } if (bImport && !opt.ReadOnly) { while (true) { try { pbResult = File.ReadAllBytes(strFile); break; } catch (Exception exRead) { if (!AskForRetry(strFile, exRead.Message)) { break; } } } } string strReportObj = null; while (true) { try { strReportObj = strFile; if (File.Exists(strFile)) { FileInfo fiTemp = new FileInfo(strFile); long cb = fiTemp.Length; if (cb > 0) { byte[] pbOvr = new byte[cb]; Program.GlobalRandom.NextBytes(pbOvr); File.WriteAllBytes(strFile, pbOvr); } File.Delete(strFile); } strReportObj = strTempDir; if (Directory.Exists(strTempDir)) { Directory.Delete(strTempDir, true); } break; } catch (Exception exDel) { if (!AskForRetry(strReportObj, exDel.Message)) { break; } } } } catch (Exception ex) { MessageService.ShowWarning(ex); } return(pbResult); }
private static void ExecuteShellCmd(EcasAction a, EcasContext ctx) { string strCmd = EcasUtil.GetParamString(a.Parameters, 0); string strArgs = EcasUtil.GetParamString(a.Parameters, 1, true, true); bool bWait = EcasUtil.GetParamBool(a.Parameters, 2); uint uWindowStyle = EcasUtil.GetParamUInt(a.Parameters, 3); string strVerb = EcasUtil.GetParamString(a.Parameters, 4, true); if (string.IsNullOrEmpty(strCmd)) { return; } Process p = null; try { PwEntry pe = null; try { pe = Program.MainForm.GetSelectedEntry(false); } catch (Exception) { Debug.Assert(false); } strCmd = WinUtil.CompileUrl(strCmd, pe, true, null, false); ProcessStartInfo psi = new ProcessStartInfo(); psi.FileName = NativeLib.EncodePath(strCmd); if (!string.IsNullOrEmpty(strArgs)) { psi.Arguments = strArgs; } bool bShEx = true; if (!string.IsNullOrEmpty(strVerb)) { } // Need ShellExecute else if ((uWindowStyle == IdWindowMin) || (uWindowStyle == IdWindowMax)) { } // Need ShellExecute else { string strCmdFlt = strCmd.TrimEnd(new char[] { '\"', '\'', ' ', '\t', '\r', '\n' }); if (strCmdFlt.EndsWith(".exe", StrUtil.CaseIgnoreCmp) || strCmdFlt.EndsWith(".com", StrUtil.CaseIgnoreCmp)) { bShEx = false; } } psi.UseShellExecute = bShEx; if (uWindowStyle == IdWindowHidden) { psi.CreateNoWindow = true; psi.WindowStyle = ProcessWindowStyle.Hidden; } else if (uWindowStyle == IdWindowMin) { psi.WindowStyle = ProcessWindowStyle.Minimized; } else if (uWindowStyle == IdWindowMax) { psi.WindowStyle = ProcessWindowStyle.Maximized; } if (!string.IsNullOrEmpty(strVerb)) { psi.Verb = strVerb; } p = Process.Start(psi); if ((p != null) && bWait) { Program.MainForm.UIBlockInteraction(true); MessageService.ExternalIncrementMessageCount(); try { p.WaitForExit(); } catch (Exception) { Debug.Assert(false); } MessageService.ExternalDecrementMessageCount(); Program.MainForm.UIBlockInteraction(false); } } catch (Exception ex) { throw new Exception(strCmd + MessageService.NewParagraph + ex.Message); } finally { try { if (p != null) { p.Dispose(); } } catch (Exception) { Debug.Assert(false); } } }