在C#中怎么样编写代码实现当windows启动时程序自动运行?谢谢

2024-11-14 14:45:12
推荐回答(3个)
回答1:

RunWhenStart(false, Application.ProductName, Application.StartupPath + @"\ToolsByJack.exe");

///


/// 开机启动项
///

/// 是否启动
/// 启动值的名称
/// 启动程序的路径
public static void RunWhenStart(bool Started, string name, string path)
{
RegistryKey HKLM = Registry.LocalMachine;
RegistryKey Run = HKLM.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
if (Started == true)
{
try
{
Run.SetValue(name, path);
HKLM.Close();
}
catch (Exception Err)
{
System.Windows.Forms.MessageBox.Show(err.Message, "ToolsByJack", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error); }
} else
{
try
{
if (Run.GetValue(name) != null)
{
Run.DeleteKey(name,false);
HKLM.Close();
} }
catch (Exception)
{
//
}
}
}
c#开机自动启动程序实现代码

回答2:

1.放到启动文件夹
2.添加注册表的run值
3.为C#程序编写一个服务,通过服务调用
4.DLL插入进程explorer,进行调用

回答3:

做成winservice