C#开机自动启动程序
来源:互联网 发布:电销数据是什么 编辑:程序博客网 时间:2024/06/11 15:03
开机自动启动程序的函数:
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); //取消自动启动
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); //取消自动启动
0 0
- C#开机自动启动程序
- C# 开机自动启动程序
- c#开机自动启动程序实现代码
- c#开机自动启动程序实现代码
- c#开机自动启动程序实现代码
- c#开机自动启动程序实现代码
- c#下实现程序开机自动启动
- c#下实现程序开机自动启动
- 程序开机自动启动
- 开机自动启动程序
- 开机自动启动程序
- 开机自动启动程序
- 开机自动启动程序
- 开机自动启动程序
- 开机自动启动程序
- C# 程序开机启动
- C# 开机启动程序
- C#开机自动启动代码
- 如何用C#语言通过服务来实现开机启动?
- 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示
- 使用opencv的repeat实现matlab的meshgrid。
- git设置ssh详细教程
- 2015百度之星资格赛1006_单调区间
- C#开机自动启动程序
- Java调用Zebra条码打印机打印条码、中英文数字条码混合标签,可自由控制格式和排版(2)
- powershell各种反弹姿势以及取证(二)
- Android 中Application类的用法
- 单例模式的简单介绍
- 抽象数据类型ADT
- DBCP数据库连接池原理分析
- maven+springMVC+mybatis+junit详细搭建过程
- 滑动刻度尺 身高体重腰围