c#实现程序的开机启动大致有两种方法,就是写入注册表或者采用服务程序,最近一直研究着用C#来操作注册表,下面介绍的方法便是用注册表来实现程序随开机启动。
关键代码很短,如下:
前提要引入命名空间 using Microsoft.Win32;
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
key.SetValue(程序的名称, 程序的路径);//设置为开机启动
为了安全性,还应该判断设置的文件是否存在以及该子项是否存在,所有完整代码如下:

Code
using Microsoft.Win32;

private void btnShowOpen_Click(object sender, EventArgs e)

{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "可执行文件(*.exe)|*.exe";
if (open.ShowDialog() == DialogResult.OK)

{
txtPath.Text = open.FileName;
}
}

private bool runWhenStart(bool started,string exeName, string path)

{
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项
if (key == null)//如果该项不存在的话,则创建该子项

{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
if (started == true)

{
try

{
key.SetValue(exeName, path);//设置为开机启动
key.Close();
}
catch

{
return false;
}
}
else

{
try

{
key.DeleteValue(exeName);//取消开机启动
key.Close();
}
catch

{
return false;
}
}
return true;
}

private void btnSet_Click(object sender, EventArgs e)

{
if (txtPath.Text == "")//检查是否选择了文件

{
MessageBox.Show("请选择要随计算机一起启动的程序路径!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtPath.Focus();
return;
}
string path = txtPath.Text.Trim();
string exeName = path.Substring(path.LastIndexOf("\\") + 1);
if (!File.Exists(path))//检查该文件是否存在

{
MessageBox.Show("文件不存在!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtPath.Text = "";
txtPath.Focus();
return;
}
if (runWhenStart(true,exeName, path))

{
MessageBox.Show("设置成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else

{
MessageBox.Show("设置失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

private void btnCancel_Click(object sender, EventArgs e)

{
if (txtPath.Text == "")//检查是否选择了文件

{
MessageBox.Show("请选择要撤销随计算机一起启动的程序路径!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtPath.Focus();
return;
}
string path = txtPath.Text.Trim();
string exeName = path.Substring(path.LastIndexOf("\\") + 1);
if (!File.Exists(path))//检查该文件是否存在

{
MessageBox.Show("文件不存在!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
txtPath.Text = "";
txtPath.Focus();
return;
}
if (runWhenStart(false, exeName, path))

{
MessageBox.Show("撤销成功!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else

{
MessageBox.Show("撤销失败!", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)