winform(c#) 开机自动启动程序
2009-09-14 15:22
561 查看
public void SetAutoRun(string fileName, bool isAutoRun)
{
RegistryKey reg = null;
try
{
if (!System.IO.File.Exists(fileName))
throw new Exception("该文件不存在!");
String name = fileName.Substring(fileName.LastIndexOf(@"/") + 1);
reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run", true);
if (reg == null)
reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run");
if (isAutoRun)
reg.SetValue(name, fileName);
else
reg.SetValue(name, false);
lbl_autorunerr.Visible = false;
}
catch
{
lbl_autorunerr.Visible = true;
//throw new Exception(ex.ToString());
}
finally
{
if (reg != null)
reg.Close();
}
}
public void SetAutoRun(string fileName, bool isAutoRun)
{
RegistryKey reg = null;
try
{
if (!System.IO.File.Exists(fileName))
throw new Exception("该文件不存在!");
String name = fileName.Substring(fileName.LastIndexOf(@"/") + 1);
reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run", true);
if (reg == null)
reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run");
if (isAutoRun)
reg.SetValue(name, fileName);
else
reg.SetValue(name, false);
lbl_autorunerr.Visible = false;
}
catch
{
lbl_autorunerr.Visible = true;
//throw new Exception(ex.ToString());
}
finally
{
if (reg != null)
reg.Close();
}
}
使用的时候,直接调用SetAutoRun函数即可,这里解释一下两个参数的含义:
fileName:需要设置自动启动程序的路径,若为当前程序可直接传递Application.ExecutablePath。
isAutoRun:是否自动运行,为false时,取消自动运行。
例: SetAutoRun(Application.ExecutablePath, true); //设置自动启动当前程序
SetAutoRun(Application.ExecutablePath, false); //取消自动启动
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cbq926/archive/2009/02/23/3930257.aspx
{
RegistryKey reg = null;
try
{
if (!System.IO.File.Exists(fileName))
throw new Exception("该文件不存在!");
String name = fileName.Substring(fileName.LastIndexOf(@"/") + 1);
reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run", true);
if (reg == null)
reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run");
if (isAutoRun)
reg.SetValue(name, fileName);
else
reg.SetValue(name, false);
lbl_autorunerr.Visible = false;
}
catch
{
lbl_autorunerr.Visible = true;
//throw new Exception(ex.ToString());
}
finally
{
if (reg != null)
reg.Close();
}
}
public void SetAutoRun(string fileName, bool isAutoRun)
{
RegistryKey reg = null;
try
{
if (!System.IO.File.Exists(fileName))
throw new Exception("该文件不存在!");
String name = fileName.Substring(fileName.LastIndexOf(@"/") + 1);
reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run", true);
if (reg == null)
reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE/Microsoft/Windows/CurrentVersion/Run");
if (isAutoRun)
reg.SetValue(name, fileName);
else
reg.SetValue(name, false);
lbl_autorunerr.Visible = false;
}
catch
{
lbl_autorunerr.Visible = true;
//throw new Exception(ex.ToString());
}
finally
{
if (reg != null)
reg.Close();
}
}
使用的时候,直接调用SetAutoRun函数即可,这里解释一下两个参数的含义:
fileName:需要设置自动启动程序的路径,若为当前程序可直接传递Application.ExecutablePath。
isAutoRun:是否自动运行,为false时,取消自动运行。
例: SetAutoRun(Application.ExecutablePath, true); //设置自动启动当前程序
SetAutoRun(Application.ExecutablePath, false); //取消自动启动
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cbq926/archive/2009/02/23/3930257.aspx
相关文章推荐
- winform(c#) 开机自动启动程序
- 两个小知识:C#如何设置开机启动时自动执行程序|C# WinForm打开超链接
- C#程序实现软件开机自动启动的两种常用方法
- winform程序开机自动启动代码
- c#开机自动启动程序实现代码
- C#客户端(WinForm)开机自动启动实现
- C# winForm程序开机启动和托盘显示 (转http://blog.csdn.net/xinyue3054/article/details/6599508)
- C#实现winform软件开机自动启动并最小化到系统托盘
- C# winForm程序开机启动和托盘显示
- 让你写的c#程序一开机就自动运行 Windows服务实现开机启动
- C# Winform 开机自动启动
- Winform 开机自动启动(C#)
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动
- c#怎样打包 可以使另一个程序随开机自动启动
- C#.NET Form设置/取消开机自动运行,判断程序是否已经设置成开机自动启动
- C# 开机自动启动程序
- [WinForm]写一个小程序把指定目录的程序添加到开机自动启动(无法绕过360检查)
- Winform 开机自动启动(C#)
- c#开机自动启动程序实现代码
- C# winForm程序开机启动和托盘显示