C#.NET Form设置/取消开机自动运行

本文介绍了一个简单的应用程序如何通过修改注册表来实现开机自动运行的功能。文章详细展示了如何使用C#代码检查并设置程序开机启动的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建于 2014-12-15

迁移自本人的百度空间

------------------------------

1、在Form_Load触发事件里加入如下代码

// 判断程序是否已经设置成开机自动启动

RegistryKey local_chek = Registry.LocalMachine;

RegistryKey run_Check = local_chek.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

if (run_Check.GetValue("RobotPC").ToString().ToLower() != "false")

{

    this.chkBoxAutoRun.Checked = true;

}

else

{

    this.chkBoxAutoRun.Checked = false;

}

2、添加chkBoxAutoRun复选框,并在其触发事件中实现如下代码

// 开机自动运行

private void AutoRun()

{

    try

    {

        //获取程序执行路径..

        string starupPath = Application.ExecutablePath;

        //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装.

        RegistryKey local = Registry.LocalMachine;

        RegistryKey run = local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

             

        //SetValue:存储值的名称

        if (gbAutoRun == false)

        {

            run.SetValue("RobotPC", false);     // 取消开机运行

        }

        else

        {

            run.SetValue("RobotPC", starupPath);   // 设置开机运行

        }

 

        local.Close();

    }

    catch (Exception ex)

    {

        MessageBox.Show(ex.Message,

                        "错误",

                        MessageBoxButtons.OK,

                        MessageBoxIcon.Exclamation);

    }

}

 

private void chkBoxAutoRun_CheckedChanged(object sender, EventArgs e)

{

    gbAutoRun = this.chkBoxAutoRun.Checked;  // gbAutoRub是全局变量

    AutoRun();

}

 

有关调用实时(JIT)调试而不是此对话框的详细信息, 请参见此消息的结尾。 ************** 异常文本 ************** System.UnauthorizedAccessException: 拒绝访问。 (异常来自 HRESULT:0x80070005 (E_ACCESSDENIED)) 在 IWshRuntimeLibrary.IWshShortcut.Save() 在 StartUp.StartupApp.button2_Click(Object sender, EventArgs e) 位置 D:\VS2022\C#StartUp开机启动\StartUp\StartUp\Form1.cs:行号 148 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 在 System.Windows.Forms.Control.WndProc(Message& m) 在 System.Windows.Forms.ButtonBase.WndProc(Message& m) 在 System.Windows.Forms.Button.WndProc(Message& m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) ************** 已加载的程序集 ************** mscorlib 程序集版本:4.0.0.0 Win32 版本:4.8.9310.0 built by: NET481REL1LAST_C 基本代码:file:///C:/Windows/Microsoft.NET/Framework64/v4.0.30319/mscorlib.dll ---------------------------------------- StartUp 程序集版本:1.0.0.0 Win32 版本:1.0.0.0 基本代码:file:///D:/VS2022/C%23StartUp%E5%BC%80%E6%9C%BA%E5%90%AF%E5%8A%A8/StartUp/StartUp/bin/Debug/StartUp.exe ---------------------------------------- System.Windows.Forms 程序集版本:4.0.0.0 Win32 版本:4.8.9251.0 built by: NET481REL1LAST_C 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll ---------------------------------------- System 程序集版本:4.0.0.0 Win32 版本:4.8.9310.0 built by: NET481REL1LAST_C 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll ---------------------------------------- System.Drawing 程序集版本:4.0.0.0 Win32 版本:4.8.9037.0 built by: NET481REL1 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll ---------------------------------------- System.Configuration 程序集版本:4.0.0.0 Win32 版本:4.8.9037.0 built by: NET481REL1 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll ---------------------------------------- System.Core 程序集版本:4.0.0.0 Win32 版本:4.8.9297.0 built by: NET481REL1LAST_C 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll ---------------------------------------- System.Xml 程序集版本:4.0.0.0 Win32 版本:4.8.9037.0 built by: NET481REL1 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll ---------------------------------------- Accessibility 程序集版本:4.0.0.0 Win32 版本:4.8.9037.0 built by: NET481REL1 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll ---------------------------------------- Microsoft.CSharp 程序集版本:4.0.0.0 Win32 版本:4.8.9037.0 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.CSharp/v4.0_4.0.0.0__b03f5f7f11d50a3a/Microsoft.CSharp.dll ---------------------------------------- System.Dynamic 程序集版本:4.0.0.0 Win32 版本:4.8.9037.0 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Dynamic/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Dynamic.dll ---------------------------------------- Anonymously Hosted DynamicMethods Assembly 程序集版本:0.0.0.0 Win32 版本:4.8.9310.0 built by: NET481REL1LAST_C 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_64/mscorlib/v4.0_4.0.0.0__b77a5c561934e089/mscorlib.dll ---------------------------------------- mscorlib.resources 程序集版本:4.0.0.0 Win32 版本:4.8.9037.0 built by: NET481REL1 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/mscorlib.resources.dll ---------------------------------------- System.Windows.Forms.resources 程序集版本:4.0.0.0 Win32 版本:4.8.9037.0 built by: NET481REL1 基本代码:file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/System.Windows.Forms.resources.dll ---------------------------------------- ************** JIT 调试 ************** 要启用实时(JIT)调试, 该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置 jitDebugging 值。 编译应用程序时还必须启用 调试。 例如: <configuration> <system.windows.forms jitDebugging="true" /> </configuration> 启用 JIT 调试后,任何未经处理的异常 都将被发送到在此计算机上注册的 JIT 调试程序, 而不是由此对话框处理。
最新发布
06-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值