C#编写开机自动运行函数
2008-04-21 17:21
393 查看
/// <summary>
/// 开机启动项
/// </summary>
/// <param name="Started">是否启动</param>
/// <param name="name">启动值的名称</param>
/// <param name="path">启动程序的路径</param>
public static void RunWhenStart(bool Started, string name, string path)
{
using (RegistryKey HKLM = Registry.LocalMachine)
{
using (RegistryKey Run = HKLM.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run",RegistryKeyPermissionCheck.ReadWriteSubTree))
{
if (Started)
{
try
{
Run.SetValue(name, path);
}
catch (Exception Err)
{
MessageBox.Show(Err.Message.ToString(), "出错啦!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
try
{
Run.DeleteValue(name);
}
catch (Exception Err)
{
MessageBox.Show(Err.Message.ToString(), "出错啦!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
调用方式:
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
string MyKey = "ForumsToolServiceMonitor";
string Path = Application.StartupPath + @"/ForumsToolServiceMonitor.exe";
if (toolStripMenuItem2.Checked)
{
ForumsToolServiceMonitor.RunWhenStart(true, MyKey, Path);
}
else
{
ForumsToolServiceMonitor.RunWhenStart(false, MyKey, Path);
}
}
/// 开机启动项
/// </summary>
/// <param name="Started">是否启动</param>
/// <param name="name">启动值的名称</param>
/// <param name="path">启动程序的路径</param>
public static void RunWhenStart(bool Started, string name, string path)
{
using (RegistryKey HKLM = Registry.LocalMachine)
{
using (RegistryKey Run = HKLM.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run",RegistryKeyPermissionCheck.ReadWriteSubTree))
{
if (Started)
{
try
{
Run.SetValue(name, path);
}
catch (Exception Err)
{
MessageBox.Show(Err.Message.ToString(), "出错啦!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
try
{
Run.DeleteValue(name);
}
catch (Exception Err)
{
MessageBox.Show(Err.Message.ToString(), "出错啦!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
}
调用方式:
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
string MyKey = "ForumsToolServiceMonitor";
string Path = Application.StartupPath + @"/ForumsToolServiceMonitor.exe";
if (toolStripMenuItem2.Checked)
{
ForumsToolServiceMonitor.RunWhenStart(true, MyKey, Path);
}
else
{
ForumsToolServiceMonitor.RunWhenStart(false, MyKey, Path);
}
}
相关文章推荐
- 详解C#开发开机自动运行函数
- C# 关于开机自动运行程序方式之一
- 朋友的一个问题:Linux开机如何自动运行自己编写好的shell脚本
- c#设置软件开机自动运行,修改注册表
- Linux开机如何自动运行自己编写好的shell脚本
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动
- C#中设置开机自动运行和关机
- C#操作注册表及设置开机自动运行
- C#操作注册表及设置开机自动运行
- C# 设置程序开机自动运行/设置&获取程序的安装路径
- C# 设置程序开机自动运行/设置&获取程序的安装路径
- c#设置软件开机自动运行,修改注册表
- C# WinForm 开机自动运行
- C#中设置开机自动运行和关机
- C#中设置开机自动运行和关机
- C#中设置开机自动运行和关机
- [转]C#写的程序如何在开机的时候自动运行
- Linux服务器Jboss运行环境搭建步骤和开机自动启动脚本编写运行
- C# winform利用注册表信息实现开机自动运行软件
- C# WinForm 开机自动运行