コード例 #1
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);
                }
            }
        }
コード例 #2
0
        public static bool FlushStorageBuffers(char chDriveLetter, bool bOnlyIfRemovable)
        {
            string strDriveLetter = new string(chDriveLetter, 1);
            bool   bResult        = true;

            try
            {
                if (bOnlyIfRemovable)
                {
                    DriveInfo di = new DriveInfo(strDriveLetter);
                    if (di.DriveType != DriveType.Removable)
                    {
                        return(true);
                    }
                }

                string strDevice = "\\\\.\\" + strDriveLetter + ":";

                IntPtr hDevice = NativeMethods.CreateFile(strDevice,
                                                          NativeMethods.EFileAccess.GenericRead | NativeMethods.EFileAccess.GenericWrite,
                                                          NativeMethods.EFileShare.Read | NativeMethods.EFileShare.Write,
                                                          IntPtr.Zero, NativeMethods.ECreationDisposition.OpenExisting,
                                                          0, IntPtr.Zero);
                if (NativeMethods.IsInvalidHandleValue(hDevice))
                {
                    Debug.Assert(false);
                    return(false);
                }

                string strDir = FreeDriveIfCurrent(chDriveLetter);

                uint dwDummy;
                if (NativeMethods.DeviceIoControl(hDevice, NativeMethods.FSCTL_LOCK_VOLUME,
                                                  IntPtr.Zero, 0, IntPtr.Zero, 0, out dwDummy, IntPtr.Zero) != false)
                {
                    if (NativeMethods.DeviceIoControl(hDevice, NativeMethods.FSCTL_UNLOCK_VOLUME,
                                                      IntPtr.Zero, 0, IntPtr.Zero, 0, out dwDummy, IntPtr.Zero) == false)
                    {
                        Debug.Assert(false);
                    }
                }
                else
                {
                    bResult = false;
                }

                if (strDir.Length > 0)
                {
                    WinUtil.SetWorkingDirectory(strDir);
                }

                if (!NativeMethods.CloseHandle(hDevice))
                {
                    Debug.Assert(false);
                }
            }
            catch (Exception)
            {
                Debug.Assert(false);
                return(false);
            }

            return(bResult);
        }
コード例 #3
0
        public static void OpenUrl(string strUrlToOpen, PwEntry peDataSource,
                                   bool bAllowOverride, string strBaseRaw)
        {
            // If URL is null, return, do not throw exception.
            Debug.Assert(strUrlToOpen != null); if (strUrlToOpen == null)
            {
                return;
            }

            string strPrevWorkDir = WinUtil.GetWorkingDirectory();
            string strThisExe     = WinUtil.GetExecutable();

            string strExeDir = UrlUtil.GetFileDirectory(strThisExe, false, true);

            WinUtil.SetWorkingDirectory(strExeDir);

            string strUrlFlt = strUrlToOpen;

            strUrlFlt = strUrlFlt.TrimStart(new char[] { ' ', '\t', '\r', '\n' });

            PwDatabase pwDatabase = null;

            try
            {
                pwDatabase = Program.MainForm.DocumentManager.SafeFindContainerOf(
                    peDataSource);
            }
            catch (Exception) { Debug.Assert(false); }

            bool bCmdQuotes = WinUtil.IsCommandLineUrl(strUrlFlt);

            SprContext ctx = new SprContext(peDataSource, pwDatabase,
                                            SprCompileFlags.All, false, bCmdQuotes);

            ctx.Base          = strBaseRaw;
            ctx.BaseIsEncoded = false;

            string strUrl = SprEngine.Compile(strUrlFlt, ctx);

            string strOvr = Program.Config.Integration.UrlSchemeOverrides.GetOverrideForUrl(
                strUrl);

            if (!bAllowOverride)
            {
                strOvr = null;
            }
            if (strOvr != null)
            {
                bool bCmdQuotesOvr = WinUtil.IsCommandLineUrl(strOvr);

                SprContext ctxOvr = new SprContext(peDataSource, pwDatabase,
                                                   SprCompileFlags.All, false, bCmdQuotesOvr);
                ctxOvr.Base          = strUrl;
                ctxOvr.BaseIsEncoded = bCmdQuotes;

                strUrl = SprEngine.Compile(strOvr, ctxOvr);
            }

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

                try
                {
                    if ((strArgs != null) && (strArgs.Length > 0))
                    {
                        Process.Start(strApp, strArgs);
                    }
                    else
                    {
                        Process.Start(strApp);
                    }
                }
                catch (Win32Exception)
                {
                    StartWithoutShellExecute(strApp, strArgs);
                }
                catch (Exception exCmd)
                {
                    string strInf = KPRes.FileOrUrl + ": " + strApp;
                    if ((strArgs != null) && (strArgs.Length > 0))
                    {
                        strInf += MessageService.NewParagraph +
                                  KPRes.Arguments + ": " + strArgs;
                    }

                    MessageService.ShowWarning(strInf, exCmd);
                }
            }
            else             // Standard URL
            {
                try { Process.Start(strUrl); }
                catch (Exception exUrl)
                {
                    MessageService.ShowWarning(strUrl, exUrl);
                }
            }

            // Restore previous working directory
            WinUtil.SetWorkingDirectory(strPrevWorkDir);

            // SprEngine.Compile might have modified the database
            Program.MainForm.UpdateUI(false, null, false, null, false, null, false);
        }
コード例 #4
0
ファイル: WinUtil.cs プロジェクト: jonbride/strengthreport
        public static void OpenUrl(string strUrlToOpen, PwEntry peDataSource)
        {
            // If URL is null, return false, do not throw exception.
            Debug.Assert(strUrlToOpen != null); if (strUrlToOpen == null)
            {
                return;
            }

            string strPrevWorkDir = Directory.GetCurrentDirectory();
            string strThisExe     = WinUtil.GetExecutable();

            string strExeDir = UrlUtil.GetFileDirectory(strThisExe, false);

            try { Directory.SetCurrentDirectory(strExeDir); }
            catch (Exception) { Debug.Assert(false); }

            string strUrl = strUrlToOpen;

            strUrl = strUrl.TrimStart(new char[] { ' ', '\t', '\r', '\n' });

            PwDatabase pwDatabase = null;

            try { pwDatabase = Program.MainForm.PluginHost.Database; }
            catch (Exception) { Debug.Assert(false); pwDatabase = null; }

            bool bCmdQuotes = WinUtil.IsCommandLineUrl(strUrl);

            strUrl = SprEngine.Compile(strUrl, false, peDataSource, pwDatabase,
                                       false, bCmdQuotes);

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

                try
                {
                    if ((strArgs != null) && (strArgs.Length > 0))
                    {
                        Process.Start(strApp, strArgs);
                    }
                    else
                    {
                        Process.Start(strApp);
                    }
                }
                catch (Win32Exception)
                {
                    StartWithoutShellExecute(strApp, strArgs);
                }
                catch (Exception exCmd)
                {
                    string strInf = KPRes.FileOrUrl + ": " + strApp;
                    if ((strArgs != null) && (strArgs.Length > 0))
                    {
                        strInf += MessageService.NewParagraph +
                                  KPRes.Arguments + ": " + strArgs;
                    }

                    MessageService.ShowWarning(strInf, exCmd);
                }
            }
            else
            {
                try { Process.Start(strUrl); }
                catch (Exception exUrl)
                {
                    MessageService.ShowWarning(strUrl, exUrl);
                }
            }

            // Restore previous working directory
            try { Directory.SetCurrentDirectory(strPrevWorkDir); }
            catch (Exception) { Debug.Assert(false); }
        }
コード例 #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
        private static void OpenUrlPriv(string strUrlToOpen, PwEntry peDataSource,
                                        bool bAllowOverride, string strBaseRaw)
        {
            if (strUrlToOpen == null)
            {
                Debug.Assert(false); 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);

            Process p = null;

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

                try
                {
                    if (!string.IsNullOrEmpty(strArgs))
                    {
                        p = Process.Start(strApp, strArgs);
                    }
                    else
                    {
                        p = Process.Start(strApp);
                    }
                }
                catch (Win32Exception)
                {
                    StartWithoutShellExecute(strApp, strArgs);
                }
                catch (Exception exCmd)
                {
                    string strInf = KPRes.FileOrUrl + ": " + strApp;
                    if ((strArgs != null) && (strArgs.Length > 0))
                    {
                        strInf += StrUtil.NewParagraph +
                                  KPRes.Arguments + ": " + strArgs;
                    }

                    MessageService.ShowWarning(strInf, exCmd);
                }
            }
            else             // Standard URL
            {
                try { p = Process.Start(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);
            }
        }
コード例 #8
0
        private static void OnDownloadCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            string strXmlFile = NetUtil.GZipUtf8ResultToString(e);

            if (strXmlFile == null)
            {
                if (e.Error != null)
                {
                    if (m_tsResultsViewer == null)
                    {
                        MessageService.ShowWarning(KPRes.UpdateCheckingFailed, e.Error);
                    }
                    else if (e.Error.Message != null)
                    {
                        CheckForUpdate.SetTsStatus(KPRes.UpdateCheckingFailed + " " +
                                                   e.Error.Message);
                    }
                }
                else
                {
                    ReportStatusEx(KPRes.UpdateCheckingFailed, true);
                }

                return;
            }

            XmlDocument xmlDoc = new XmlDocument();

            try { xmlDoc.LoadXml(strXmlFile); }
            catch (Exception)
            {
                StructureFail();
                return;
            }

            XmlElement xmlRoot = xmlDoc.DocumentElement;

            if (xmlRoot == null)
            {
                StructureFail(); return;
            }
            if (xmlRoot.Name != ElemRoot)
            {
                StructureFail(); return;
            }

            uint uVersion = 0;

            foreach (XmlNode xmlChild in xmlRoot.ChildNodes)
            {
                if (xmlChild.Name == ElemVersionU)
                {
                    uint.TryParse(xmlChild.InnerText, out uVersion);
                }
                else if (xmlChild.Name == ElemVersionStr)
                {
                    // strVersion = xmlChild.InnerText;
                }
            }

            if (uVersion > PwDefs.Version32)
            {
                if (m_tsResultsViewer == null)
                {
                    if (MessageService.AskYesNo(KPRes.ChkForUpdNewVersion +
                                                MessageService.NewParagraph + KPRes.HomepageVisitQuestion))
                    {
                        WinUtil.OpenUrl(PwDefs.HomepageUrl, null);
                    }
                }
                else
                {
                    CheckForUpdate.SetTsStatus(KPRes.ChkForUpdNewVersion);
                }
            }
            else if (uVersion == PwDefs.Version32)
            {
                ReportStatusEx(KPRes.ChkForUpdGotLatest, false);
            }
            else
            {
                ReportStatusEx(KPRes.UnknownFileVersion, true);
            }
        }