コード例 #1
0
        private static void ShellOpenFn(object o)
        {
            if (o == null)
            {
                Debug.Assert(false); return;
            }

            try
            {
                ProcessStartInfo psi = (o as ProcessStartInfo);
                if (psi == null)
                {
                    Debug.Assert(false); return;
                }

                // Let the main thread finish showing the message box
                Thread.Sleep(200);

                NativeLib.StartProcess(psi);
            }
            catch (Exception ex)
            {
                try { MessageService.ShowWarning(ex); }
                catch (Exception) { Debug.Assert(false); }
            }
        }
コード例 #2
0
        internal static void OpenUrlDirectly(string strUrl)
        {
            if (string.IsNullOrEmpty(strUrl))
            {
                Debug.Assert(false); return;
            }

            try { NativeLib.StartProcess(strUrl); }
            catch (Exception ex) { MessageService.ShowWarning(strUrl, ex); }
        }
コード例 #3
0
        public static bool RunElevated(string strExe, string strArgs,
                                       bool bShowMessageIfFailed)
        {
            if (strExe == null)
            {
                throw new ArgumentNullException("strExe");
            }

            try
            {
                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = strExe;
                if (!string.IsNullOrEmpty(strArgs))
                {
                    psi.Arguments = strArgs;
                }
                psi.UseShellExecute = true;

                // Elevate on Windows Vista and higher
                if (WinUtil.IsAtLeastWindowsVista)
                {
                    psi.Verb = "runas";
                }

                NativeLib.StartProcess(psi);
            }
            catch (Exception ex)
            {
                if (bShowMessageIfFailed)
                {
                    MessageService.ShowWarning(ex);
                }
                return(false);
            }

            return(true);
        }
コード例 #4
0
ファイル: WinUtil.cs プロジェクト: Gallimathias/KeePass
        internal static void ShowFileInFileManager(string strFilePath, bool bShowError)
        {
            if (string.IsNullOrEmpty(strFilePath))
            {
                Debug.Assert(false); return;
            }

            try
            {
                string strDir = UrlUtil.GetFileDirectory(strFilePath, false, true);
                if (NativeLib.IsUnix())
                {
                    NativeLib.StartProcess(strDir);
                    return;
                }

                string strExplorer = WinUtil.LocateSystemApp("Explorer.exe");

                if (File.Exists(strFilePath))
                {
                    NativeLib.StartProcess(strExplorer, "/select,\"" +
                                           NativeLib.EncodeDataToArgs(strFilePath) + "\"");
                }
                else
                {
                    NativeLib.StartProcess(strDir);
                }
            }
            catch (Exception ex)
            {
                if (bShowError)
                {
                    MessageService.ShowWarning(strFilePath, ex.Message);
                }
            }
        }
コード例 #5
0
 public static void Restart()
 {
     try { NativeLib.StartProcess(WinUtil.GetExecutable()); }
     catch (Exception ex) { MessageService.ShowWarning(ex); }
 }
コード例 #6
0
        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);

            if (WinUtil.IsCommandLineUrl(strUrl))
            {
                string strApp, strArgs;
                StrUtil.SplitCommandLine(WinUtil.GetCommandLineFromUrl(strUrl),
                                         out strApp, out strArgs);

                try
                {
                    try { NativeLib.StartProcess(strApp, strArgs); }
                    catch (Win32Exception)
                    {
                        ProcessStartInfo psi = new ProcessStartInfo();
                        psi.FileName = strApp;
                        if (!string.IsNullOrEmpty(strArgs))
                        {
                            psi.Arguments = strArgs;
                        }
                        psi.UseShellExecute = false;

                        NativeLib.StartProcess(psi);
                    }
                }
                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 { NativeLib.StartProcess(strUrl); }
                catch (Exception exUrl)
                {
                    MessageService.ShowWarning(strUrl, exUrl);
                }
            }

            // 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);
            }
        }
コード例 #7
0
ファイル: ExportUtil.cs プロジェクト: t00/KeePassCore
        public static bool Export(PwExportInfo pwExportInfo, FileFormatProvider fileFormat,
                                  IOConnectionInfo iocOutput, IStatusLogger slLogger)
        {
            if (pwExportInfo == null)
            {
                throw new ArgumentNullException("pwExportInfo");
            }
            if (pwExportInfo.DataGroup == null)
            {
                throw new ArgumentException();
            }
            if (fileFormat == null)
            {
                throw new ArgumentNullException("fileFormat");
            }

            bool bFileReq = fileFormat.RequiresFile;

            if (bFileReq && (iocOutput == null))
            {
                throw new ArgumentNullException("iocOutput");
            }
            if (bFileReq && (iocOutput.Path.Length == 0))
            {
                throw new ArgumentException();
            }

            PwDatabase pd = pwExportInfo.ContextDatabase;

            Debug.Assert(pd != null);

            if (!AppPolicy.Try(AppPolicyId.Export))
            {
                return(false);
            }
            if (!AppPolicy.Current.ExportNoKey && (pd != null))
            {
                if (!KeyUtil.ReAskKey(pd, true))
                {
                    return(false);
                }
            }

            if (!fileFormat.SupportsExport)
            {
                return(false);
            }
            if (!fileFormat.TryBeginExport())
            {
                return(false);
            }

            CompositeKey ckOrgMasterKey = null;
            DateTime     dtOrgMasterKey = PwDefs.DtDefaultNow;

            PwGroup pgOrgData     = pwExportInfo.DataGroup;
            PwGroup pgOrgRoot     = ((pd != null) ? pd.RootGroup : null);
            bool    bParentGroups = (pwExportInfo.ExportParentGroups && (pd != null) &&
                                     (pgOrgData != pgOrgRoot));

            bool bExistedAlready = true;             // No deletion by default
            bool bResult         = false;

            try
            {
                if (pwExportInfo.ExportMasterKeySpec && fileFormat.RequiresKey &&
                    (pd != null))
                {
                    KeyCreationForm kcf = new KeyCreationForm();
                    kcf.InitEx((iocOutput ?? new IOConnectionInfo()), true);

                    if (UIUtil.ShowDialogNotValue(kcf, DialogResult.OK))
                    {
                        return(false);
                    }

                    ckOrgMasterKey = pd.MasterKey;
                    dtOrgMasterKey = pd.MasterKeyChanged;

                    pd.MasterKey        = kcf.CompositeKey;
                    pd.MasterKeyChanged = DateTime.UtcNow;

                    UIUtil.DestroyForm(kcf);
                }

                if (bParentGroups)
                {
                    PwGroup pgNew = WithParentGroups(pgOrgData, pd);
                    pwExportInfo.DataGroup = pgNew;
                    pd.RootGroup           = pgNew;
                }

                if (bFileReq)
                {
                    bExistedAlready = IOConnection.FileExists(iocOutput);
                }

                Stream s = (bFileReq ? IOConnection.OpenWrite(iocOutput) : null);
                try { bResult = fileFormat.Export(pwExportInfo, s, slLogger); }
                finally { if (s != null)
                          {
                              s.Close();
                          }
                }

                if (bFileReq && bResult)
                {
                    if (pwExportInfo.ExportPostOpen)
                    {
                        NativeLib.StartProcess(iocOutput.Path);
                    }
                    if (pwExportInfo.ExportPostShow)
                    {
                        WinUtil.ShowFileInFileManager(iocOutput.Path, true);
                    }
                }
            }
            catch (Exception ex) { MessageService.ShowWarning(ex); }
            finally
            {
                if (ckOrgMasterKey != null)
                {
                    pd.MasterKey        = ckOrgMasterKey;
                    pd.MasterKeyChanged = dtOrgMasterKey;
                }

                if (bParentGroups)
                {
                    pwExportInfo.DataGroup = pgOrgData;
                    pd.RootGroup           = pgOrgRoot;
                }
            }

            if (bFileReq && !bResult && !bExistedAlready)
            {
                try { IOConnection.DeleteFile(iocOutput); }
                catch (Exception) { }
            }

            return(bResult);
        }