C# 实现是否设置指定程序为开机启动程序
2016-07-08 09:35
519 查看
/// <summary>
/// 是否设置程序为开机启动界面
/// </summary>
/// <param name="appPath">程序启动路径</param>
/// <param name="nodelist">true为设置为启动桌面,false为重新设置为操作系统界面</param>
/// <returns>修改成功返回true</returns>
public bool IsSetDesktop(string appPath, bool isSetDesktop)
{
bool ret = false;
RegistryKey reg = null;
try
{
reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System", false);
if (isSetDesktop)
{
if (null == reg)
{
reg = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System");
}
reg.SetValue("Shell", appPath);
ret = true;
}
else
{
reg.SetValue("Shell", "explorer.exe");//explorer.exe为操作系统显示桌面的exe程序
}
}
catch (Exception e)
{
Log.Exception("Set System Desktop error:" + e.Message);
}
finally
{
if (null != reg)
{
reg.Close();
}
}
return ret;
}
/// 是否设置程序为开机启动界面
/// </summary>
/// <param name="appPath">程序启动路径</param>
/// <param name="nodelist">true为设置为启动桌面,false为重新设置为操作系统界面</param>
/// <returns>修改成功返回true</returns>
public bool IsSetDesktop(string appPath, bool isSetDesktop)
{
bool ret = false;
RegistryKey reg = null;
try
{
reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System", false);
if (isSetDesktop)
{
if (null == reg)
{
reg = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Policies\System");
}
reg.SetValue("Shell", appPath);
ret = true;
}
else
{
reg.SetValue("Shell", "explorer.exe");//explorer.exe为操作系统显示桌面的exe程序
}
}
catch (Exception e)
{
Log.Exception("Set System Desktop error:" + e.Message);
}
finally
{
if (null != reg)
{
reg.Close();
}
}
return ret;
}
相关文章推荐
- C#工具类(包含获取枚举描述方法)
- C#之类型转换
- 弹出MessageBox并延迟一段时间后自动关闭的一种方法
- C#基础
- C# 判断字符串为空的几种方法 之优略选择
- 序列不包含任何元素,c#,支付宝
- C#任意变换图像大小
- C#制作、打包、签名、发布Activex全过程
- C#生成缩略图
- Visual Studio 2010 打包C#项目程序
- C# 对键值对的键或者值进行排序
- C# DataGridView自动换行
- C# 将 txt 文件拖拽到 textbox时 textbox 读取txt中文本内容
- 设置c#windows服务描述及允许服务与桌面交互的几种方法(作者博客还有一大堆C#创建服务的文章)
- C#学习随笔
- C# string转byte[]
- c# 定时器
- C#抓取天气数据
- 《C#高级编程》读书笔记(六):字符串和正则表达式
- C#使用WebService和Session