zoukankan      html  css  js  c++  java
  • C#客户端(WinForm)开机自动启动实现

    
    
            /// <summary>  
            /// 修改程序在注册表中的键值  
            /// </summary>  
            /// <param name="isAuto">true:开机启动,false:不开机自启</param> 
            private void AutoStart(bool isAuto = true, bool showinfo = true)
            {
                try
                {
                    if (isAuto == true)
                    {
                        RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
                        RegistryKey R_run = R_local.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
                        R_run.SetValue("应用名称", Application.ExecutablePath);
                        R_run.Close();
                        R_local.Close();
                    }
                    else
                    {
                        RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
                        RegistryKey R_run = R_local.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
                        R_run.DeleteValue("应用名称", false);
                        R_run.Close();
                        R_local.Close();
                    }
                }
                catch (Exception)
                {
                    if (showinfo)
                        MessageBox.Show("您需要管理员权限修改", "提示");
                }
            }

    注:该程序的启动项设置到HKEY_Current_User 下,推荐。如果想改在HKEY_LOCAL_MACHINE,只需将CurrentUser改为LocalMachine,即

    // 添加到 当前登陆用户的 注册表启动项
    RegistryKey RKey = Registry.CurrentUser.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
    RKey.SetValue("AppName", @"C:AppName.exe");
    
    // 添加到 所有用户的 注册表启动项
    RegistryKey RKey = Registry.LocalMachine.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
    RKey.SetValue("AppName", @"C:AppName.exe");
  • 相关阅读:
    .NET Framework 1.13.5 版本安装包下载链接
    可遇不可求的Question之MYSQL获取自增ID的四种方法篇
    20110917 晴
    北海道 7天6夜 自助游
    想你了
    猫忘带电话了
    [转载经验] 探亲签证申请
    帮忙打印
    打印机
    20110910 晴
  • 原文地址:https://www.cnblogs.com/mqxs/p/9475581.html
Copyright © 2011-2022 走看看