C# 如何通过浏览器外部启动应用程序 (可传参)

本文以Windows系统为例,其他系统根据环境如法炮制。

引子:

我们经常能看见一些网站可以启动本机的应用程序,我们点击网页的一个按钮后就会被重定向到一个新地址并应用程序,甚至可通过解析重定向地址内含的参数帮你实现自动化操作应用程序。

而这些网址的协议往往不是http,而是你没见过的新协议。所以我们今天搞懂这是如何实现的。

 

思路:

既然是新协议,那么肯定是有应用程序向系统配置文件定义了这个新协议,而定义方法正是通过注册表。

注册表内创建新的URL Protocol并使用Shell来启动应用程序,这样定义注册表后,那么浏览器就可通过该协议打开应用程序了。

但有一个问题,注册表写的启动路径是绝对路径,这并不适合部署到客户那边...

对,你可能已经想到了解决方案,那就是根据应用程序的路径,动态创建一个新的环境变量,然后把环境变量也注册到系统里。

然后注册表的应用程序路径使用这个环境变量就大功告成了!!

 

代码实现:

导入注册表:

 

//执行注册 构造函数
public void ExecuteReg(string path)
{
    if (File.Exists(path)) //如果注册表文件存在,则实现如下命令
    {
        //路径变换
        path = @"""" + path + @"""";
         //启动regedit.exe程序并导入注册表
        Process.Start("regedit", string.Format(" /s {0}", path));
    }
}

 

创建注册表:

       public void Writer(string savePath) {
         string[] names = new string[] {
         "Windows Registry Editor Version 5.00", 
         "",
         "[HKEY_CLASSES_ROOT\gr]",
         "@="Open gr"",
         ""URL Protocol"=""",
         "",
         "[HKEY_CLASSES_ROOT\gr\shell]",
         "",
         "[HKEY_CLASSES_ROOT\gr\shell\open]",
         "",
         "[HKEY_CLASSES_ROOT\gr\shell\open\command]",
         "",
         "@="\"%ROOT%\\名字.exe\" \"%LAUNCH_FLAGS%\"""
         };
            using (StreamWriter sw = new StreamWriter(savePath + "startup.reg"))
            {
                foreach (string s in names)
                {
                    sw.WriteLine(s);

                }
            }
        }

设置环境变量:

        public static string GetSysEnvironmentByName(string key)
        {
            string result = string.Empty;
            try
            {
                result = OpenSysEnvironment().GetValue(key).ToString();
            }
            catch (Exception)
            {
                return result;
            }
            return result;
        }
        /// <summary>
        /// 获得RegistryKey,值为
        /// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
        /// </summary>
        /// <returns></returns>
        public static RegistryKey OpenSysEnvironment()
        {
            RegistryKey regLocalMachine = Registry.LocalMachine;
            RegistryKey regSystem = regLocalMachine.OpenSubKey("System", true);
            RegistryKey regControlSet = regSystem.OpenSubKey("CurrentControlSet", true).OpenSubKey("Control",true).OpenSubKey("Session Manager",true);
            RegistryKey regEnvironment = regLocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Session Manager\Environment", true);
            return regEnvironment;
        }

        public static string SetSysEnvironment(string key, string strValue)
        {
            string pathList = null;
            if (CheckSysEnvironmentExist(key) == false)
            {
                OpenSysEnvironment().SetValue(key, strValue);
            }
            else
            {
                pathList = GetSysEnvironmentByName(key);
                if (pathList.Contains(strValue))
                {
                }
                if (!pathList.EndsWith(";"))
                {
                    OpenSysEnvironment().SetValue(key, pathList + ";");
                    pathList = GetSysEnvironmentByName(key);
                }
                else
                {
                    OpenSysEnvironment().SetValue(key, pathList + strValue);
                }
            }
        }
        /// 检测系统环境变量是否存在
        public static Boolean CheckSysEnvironmentExist(string key)
        {
            if (!string.IsNullOrEmpty(GetSysEnvironmentByName(key)))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

 

posted @ 2020-10-17 19:22  田所浩托TrosuoTro  阅读(783)  评论(0编辑  收藏  举报