注册自定义协议,通过浏览器链接打开本地应用程序

参考:http://www.cnblogs.com/leslies2/p/3727762.html

 

using System;

using System.Security.AccessControl;

using System.Windows;

using Microsoft.Win32;



namespace QPP.AutoUpdater

{

    public static class Registry

    {

        /*将协议写入注册表,以便可以通过协议打开程序*/

        /*参考http://www.cnblogs.com/leslies2/p/3727762.html*/



        //[HKEY_CLASSES_ROOT\approject]

        //@="URL:approject Protocol"

        //"URL Protocol"="D:\\Program Files\\APPGroup\\APP Project\\APP.Project.exe" "%1"

        //[HKEY_CLASSES_ROOT\qpproject\shell]

        //[HKEY_CLASSES_ROOT\qpproject\shell\open]

        //[HKEY_CLASSES_ROOT\qpproject\shell\open\command]

        //@="D:\\Program Files\\APPGroup\\APP Project\\APP.AutoUpdater.exe" "%1"



        /// <summary>

        /// 要打开的程序

        /// </summary>

        private static readonly string FilePath;

        static Registry()

        {

            FilePath = string.Format("\"{0}{1}\" \"%1\"", AppDomain.CurrentDomain.BaseDirectory, "APP.AutoUpdater.exe");

        }



        public static bool IsRegistryExit()

        {

            try

            {

                var classRootRegistryKey = Microsoft.Win32.Registry.ClassesRoot;

                var qpprojectRegistryKey = classRootRegistryKey.OpenSubKey("approject", true);

                if (qpprojectRegistryKey == null) return false;

                var shellRegistryKey = qpprojectRegistryKey.OpenSubKey("shell", true);

                if (shellRegistryKey == null) return false;

                var openRegistryKey = shellRegistryKey.OpenSubKey("open", true);

                if (openRegistryKey == null) return false;

                var commandRegistryKey = openRegistryKey.OpenSubKey("command", true);

                if (commandRegistryKey == null) return false;

                var value = commandRegistryKey.GetValue("", true);

                if (value == null) return true;

                return value.ToString().Equals(FilePath, StringComparison.OrdinalIgnoreCase);/*执行文件路径未发生改变,则判定为存在*/

            }

            catch

            {

                // ignored

            }

            return false;

        }/**/



        public static void WriteRegistry()

        {

            if (!IsRegistryExit())

            {

                GenerateRegistry();

            }

        }



        private static bool GenerateRegistry()

        {

            try

            {

                var rsy = new RegistrySecurity();



                var rar = new RegistryAccessRule(Environment.UserDomainName +

                                                 "\\" + Environment.UserName,

                    RegistryRights.ReadKey | RegistryRights.WriteKey |

                    RegistryRights.Delete, InheritanceFlags.ContainerInherit, PropagationFlags.None,

                    AccessControlType.Allow);

                rsy.AddAccessRule(rar);



                var classRootRegistryKey = Microsoft.Win32.Registry.ClassesRoot;

                var qpprojectRegistryKey = classRootRegistryKey.CreateSubKey("approject",

                    RegistryKeyPermissionCheck.ReadWriteSubTree, rsy);

                if (qpprojectRegistryKey == null) return false;

                qpprojectRegistryKey.SetValue("URL Protocol", FilePath);

                var shellRegistryKey = qpprojectRegistryKey.CreateSubKey("shell",

                    RegistryKeyPermissionCheck.ReadWriteSubTree, rsy);

                if (shellRegistryKey == null) return false;

                var openRegistryKey = shellRegistryKey.CreateSubKey("open", RegistryKeyPermissionCheck.ReadWriteSubTree,

                    rsy);

                if (openRegistryKey == null) return false;

                var commandRegistryKey = openRegistryKey.CreateSubKey("command",

                    RegistryKeyPermissionCheck.ReadWriteSubTree, rsy);

                if (commandRegistryKey == null) return false;

                commandRegistryKey.SetValue(null, FilePath);

                return true;

            }

            //catch { }

            catch (UnauthorizedAccessException exc)

            {

                MessageBox.Show("无权限操作注册表,会导致消息链接无法打开,请联系IT以管理员身份运行一次此程序。");

                //Application.Current.Shutdown();

            }

            return false;

        }

    }

}

 

你可能感兴趣的:(应用程序)