c# 开机启动项 c# 开机启动项的小例子

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

c# 开机启动项 c# 开机启动项的小例子

  2021-03-18 我要评论
想了解c# 开机启动项的小例子的相关内容吗,在本文为您仔细讲解c# 开机启动项的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:开机,启动项,下面大家一起来学习吧。

复制代码 代码如下:

  //路径, 添加开机启动/删除开机启动

        public static void SetAutoRun(string fileName, bool isAutoRun)
        {
            RegistryKey reg = null;
            try
            {
                if (!System.IO.File.Exists(fileName))
                    throw new Exception("该文件不存在!");
                String name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);
                reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (reg == null)
                    reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
                if (isAutoRun)
                    reg.SetValue(name, fileName);
                else
                    reg.SetValue(name, false);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            finally
            {
                if (reg != null)
                    reg.Close();
            }

        }

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们