欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c# 开机启动项的小例子

程序员文章站 2023-12-15 11:53:10
复制代码 代码如下:  //路径, 添加开机启动/删除开机启动         public sta...

复制代码 代码如下:

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

        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();
            }

        }

上一篇:

下一篇: