| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace HXX.Scanner.Common
- {
- public class RegisterHelper
- {
- /// <summary>
- /// 开机启动测试
- /// </summary>
- /// <param name="isAuto"></param>
- /// <param name="showinfo"></param>
- public static void AutoStart(bool isAuto = true, bool showinfo = true)
- {
- try
- {
- if (isAuto == true)
- {
- RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
- RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
- R_run.SetValue("scannerClient", Application.ExecutablePath);
- R_run.Close();
- R_local.Close();
- }
- else
- {
- RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
- RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
- R_run.DeleteValue("scannerClient", false);
- R_run.Close();
- R_local.Close();
- }
- }
- catch (Exception)
- {
- if (showinfo)
- MessageBox.Show("您需要管理员权限修改", "提示");
- }
- }
- public static bool GetRun()
- {
- //RegistryKey Key1;
- //Key1 = Registry.CurrentUser;
- //RegistryKey myreg1 = Key1.OpenSubKey("software\\AnencStatus");
- //string status = EncodeClass.Decode_base64(myreg1.GetValue("AnencStatus").ToString(), AnencKey);
- //if (status == "true")
- // return true;
- RegistryKey R_local = Registry.CurrentUser;//RegistryKey R_local = Registry.CurrentUser;
- RegistryKey R_run = R_local.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
- var re=R_run.GetValue("scannerClient");
- R_run.Close();
- R_local.Close();
- if (re == null)
- {
- return false;
- }
- return true;
- }
- public static RegistryKey CreateSubKey(RegDomain _domain,string name)
- {
- ///创建基于注册表基项的节点
- RegistryKey key = GetRegDomain(_domain);
- // 要创建的注册表项的节点
- RegistryKey sKey= IsSubKeyExist(_domain, name);
- if (sKey == null)
- {
- sKey = key.CreateSubKey(name);
- key.Close();
- return sKey;
- }
- else
- {
- key.Close();
- return sKey;
- }
- }
- public static bool WriteRegeditKey(RegistryKey key,string name,string content)
- {
- //RegistryKey software = key.CreateSubKey(key.Name);
- var software = key.OpenSubKey(key.Name, true);
- software.SetValue(name, content);
- key.Close();
- return true;
- }
- protected static RegistryKey IsSubKeyExist(RegDomain _domain, string name)
- {
- ///判断注册表项名称是否为空,如果为空,返回false
- if (name == string.Empty || name == null)
- {
- return null;
- }
- ///检索注册表子项
- ///如果sKey为null,说明没有该注册表项不存在,否则存在
- RegistryKey sKey = OpenSubKey(_domain,name);
- return sKey;
- }
- protected static RegistryKey OpenSubKey(RegDomain _domain, string name)
- {
- ///判断注册表项名称是否为空
- if (name == string.Empty || name == null)
- {
- return null;
- }
- ///创建基于注册表基项的节点
- RegistryKey key = GetRegDomain(_domain);
- ///要打开的注册表项的节点
- RegistryKey sKey = null;
- ///打开注册表项
- sKey = key.OpenSubKey(name);
- ///关闭对注册表项的更改
- key.Close();
- ///返回注册表节点
- return sKey;
- }
- protected static RegistryKey GetRegDomain(RegDomain regDomain)
- {
- ///创建基于注册表基项的节点
- RegistryKey key;
- #region 判断注册表基项域
- switch (regDomain)
- {
- case RegDomain.ClassesRoot:
- key = Registry.ClassesRoot; break;
- case RegDomain.CurrentUser:
- key = Registry.CurrentUser; break;
- case RegDomain.LocalMachine:
- key = Registry.LocalMachine; break;
- case RegDomain.User:
- key = Registry.Users; break;
- case RegDomain.CurrentConfig:
- key = Registry.CurrentConfig; break;
- case RegDomain.PerformanceData:
- key = Registry.PerformanceData; break;
- default:
- key = Registry.LocalMachine; break;
- }
- #endregion
- return key;
- }
- }
- }
|