|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using Microsoft.Win32;
-
- using System;
-
- namespace Common.system
- {
- /// <summary>
- /// 自启帮助类
- /// </summary>
- public static class AutoStartHelper
- {
- /// <summary>
- /// 开机自启动
- /// </summary>
- /// <param name="strName">需要启动软件的路径</param>
- /// <param name="Message">错误信息</param>
- /// <returns></returns>
- public static bool OpenAutoStart(string strName, out string Message)
- {
-
- #region 设置开机自启
- if (!System.IO.File.Exists(strName))//判断要自动运行的应用程序文件是否存在
- {
- Message = "未检测到应用程序启动项。";
- return false;
- }
- string strnewName = strName.Substring(strName.LastIndexOf("\\") + 1);//获取应用程序文件名,不包括路径
- try
- {
- RegistryKey registry = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//检索指定的子项
- if (registry == null)//若指定的子项不存在
- registry = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");//则创建指定的子项
- registry.SetValue(strnewName, strName);//设置该子项的新的“键值对”
- Message = "";
- return true;
- }
- catch (Exception ex)
- {
- Message = "设置失败,操作被拒绝:" + ex.Message;
- LogHelper.Logerror.Error(Message, ex);
- return false;
- }
-
- //if (MessageBox.Show("设置完毕") == DialogResult.OK)
- //{
- // RefreshSystem();//刷新系统
- //}
- #endregion
- }
- /// <summary>
- /// 关闭自启
- /// </summary>
- /// <param name="strName">软件名称</param>
- /// <param name="Message">错误消息</param>
- /// <returns></returns>
- public static bool CloseAutoStart(string strName, out string Message)
- {
- #region 取消开机自启
- if (!System.IO.File.Exists(strName))//判断要取消的应用程序文件是否存在
- {
- Message = "未检测到应用程序启动项。";
- return false;
- }
- string strnewName = strName.Substring(strName.LastIndexOf("\\") + 1);///获取应用程序文件名,不包括路径
- try
- {
- RegistryKey registry = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//读取指定的子项
- if (registry == null)//若指定的子项不存在
- registry = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");//则创建指定的子项
- registry.DeleteValue(strnewName, false);//删除指定“键名称”的键/值对
- Message = "";
- return true;
- }
- catch (Exception ex)
- {
- Message = "设置失败,操作被拒绝:" + ex.Message;
- LogHelper.Logerror.Error(Message, ex);
- return false;
- }
- //if (MessageBox.Show("设置完毕") == DialogResult.OK)
- //{
- // RefreshSystem();
- //}
- #endregion
- }
-
-
- /// <summary>
- /// 开机自启动
- /// </summary>
- /// <param name="strName">程序名称</param>
- /// <param name="Message">错误信息</param>
- /// <returns></returns>
- public static bool OpenAutoStart1(string exeName, out string Message)
- {
-
- #region 设置开机自启
- try
- {
- Message = "";
- return true;
- }
- catch (Exception ex)
- {
- Message = "设置失败,操作被拒绝:" + ex.Message;
- LogHelper.Logerror.Error(Message, ex);
- return false;
- }
-
- //if (MessageBox.Show("设置完毕") == DialogResult.OK)
- //{
- // RefreshSystem();//刷新系统
- //}
- #endregion
- }
- }
- }
|