PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > ASP.NET教程 > 正文

    C#开机自动启动软件

    作者:admin来源:王前进浏览:时间:2020-09-30 00:07:50我要评论
    导读: region 确认按钮private void btnOK_Click(object sender, EventArgs e){string FileName = "";RegistryKey folders;folders =...
    #region 确认按钮
            private void btnOK_Click(object sender, EventArgs e)
            {
                string FileName = "";
                RegistryKey folders;
                folders = OpenRegistryPath(Registry.CurrentUser, @"\software\microsoft\windows\currentversion\explorer\shell folders");
                string startupPath = folders.GetValue("Startup").ToString();
                #region 开机启动
                //如果选择开机启动
                if (cbRun.Checked)
                {
                    //需要开机启动的软件路径 + 软件名称
                    FileName = Application.StartupPath + @"\UpDownLoadService.exe";
                    FileInfo finfo = new FileInfo(FileName);
                    //菜单启动项中 创建快捷方式
                    IWshRuntimeLibrary.WshShell shell = new IWshRuntimeLibrary.WshShellClass();
                    IWshRuntimeLibrary.IWshShortcut shortcut = (IWshRuntimeLibrary.IWshShortcut)shell.CreateShortcut(startupPath + @"\服务器传输程序.lnk");
                    shortcut.TargetPath = FileName;
                    shortcut.Save();
                }
                else
                {
                    FileInfo finfo = new FileInfo(startupPath + @"\服务器传输程序.lnk");
                    finfo.Delete();
                }
                #endregion




            #region 打开注册表,写入信息
            /// <summary>
            /// 打开注册表,写入信息
            /// </summary>
            /// <param name="root">注册表封装节点</param>
            /// <param name="s">节点信息</param>
            /// <returns></returns>
            private RegistryKey OpenRegistryPath(RegistryKey root, string s)
            {
                s = s.Remove(0, 1) + @"\";
                while (s.IndexOf(@"\") != -1)
                {
                    root = root.OpenSubKey(s.Substring(0, s.IndexOf(@"\")));
                    s = s.Remove(0, s.IndexOf(@"\") + 1);
                }
                return root;
            }        #endregion  
    注释:此功能需要添加 Interop.IWshRuntimeLibrary.dll 动态库。
    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-11-253-1.html
    相关热词搜索: 自动 启动软件