C# 自启动封装

技术目标

C# 做客户端的时候,很多情况下需要自启动,如:工业环境下,工控机打开后,自动打开界面,恢复任务,所以对自启动代码段进行了封装,只要在系统启动的时候,运行一下就ok了!

技术过程

 

 public static void SelfRun(bool isAuto = true)
        {
            try
            {
                if (isAuto == true)
                {
                    RegistryKey R_local = Registry.CurrentUser;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    R_run.SetValue(Process.GetCurrentProcess().ProcessName.Replace(".vshost", ""), $"{SysConfig.ApplicationRunPath}{Process.GetCurrentProcess().ProcessName.Replace(".vshost", "")}.exe");
                    R_run.Close();
                    R_local.Close();
                }
                else
                {
                    RegistryKey R_local = Registry.CurrentUser;
                    RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                    R_run.DeleteValue(Process.GetCurrentProcess().ProcessName.Replace(".vshost", ""), false);
                    R_run.Close();
                    R_local.Close();
                }
            }
            catch (Exception ex) when (ex.Log()) { }
            catch (Exception ex)
            {
                throw ex;
            }
        }

 

  

 

技术模式

存在问题

总结

posted @ 2021-03-29 09:32  慢慢zero  阅读(159)  评论(0)    收藏  举报