/// <summary>
/// 程序开机自启动帮助类
/// </summary>
public class AutoStartHelper
{
/// <summary>
/// 设置开机自启动
/// </summary>
public static void AutoStart_Set()
{
string regPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";//注册表
string path = Application.ExecutablePath.ToLower(); //获取.exe当前程序路径
//MessageBox.Show(path);
string name = Path.GetFileName(path); //获得应用程序名称
//MessageBox.Show(name);
var regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regPath, true);
if (regKey == null)
{
regKey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(regPath);
}
regKey.SetValue(name, path);
}
/// <summary>
/// 程序自启动关闭
/// </summary>
public static void AutoStart_Close()
{
string regPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";//注册表
string path = Application.ExecutablePath.ToLower(); //获取.exe当前程序路径
//MessageBox.Show(path);
string name = Path.GetFileName(path); //获得应用程序名称
//MessageBox.Show(name);
var regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(regPath, true);
if (regKey == null)
{
regKey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(regPath);
}
if(regKey.GetValue(name) != null)
{
regKey.DeleteValue(name);
MessageBox.Show("订销存消息提醒系统,开机自启动已关闭!", "订销存消息提醒系统");
}
}
}
C# Winfrom 程序开机自启动设置
最新推荐文章于 2024-07-03 09:26:25 发布