/// <summary> /// 从注册表读取所有自动登录并启动程序的信息 /// </summary> /// <param name="infor"></param> public static void ReadAll(AutoStartInformation infor) { #region RegistryKey myKey; myKey = Registry.LocalMachine.OpenSubKey( AutoStartInformation.RegistAutoLogonPath); object tmpvalue = myKey.GetValue(AutoStartInformation .KeyAutoLogon); infor.AutologonState = (tmpvalue != null) ? (AutoStartState.GetAutoLogonState(tmpvalue.ToString())) : AutoLogonSystemState.notStart; tmpvalue = myKey.GetValue(AutoStartInformation .KeyDefaultUserName); infor.Username = (tmpvalue != null) ? (tmpvalue.ToString()) : ""; tmpvalue = myKey.GetValue(AutoStartInformation .KeyDefaultPassword); infor.Password = (tmpvalue != null) ? (tmpvalue.ToString()) : ""; myKey = Registry.CurrentUser.OpenSubKey(AutoStartInformation .RegistAutoLoadExePath); tmpvalue = myKey.GetValue(AutoStartInformation .KeyAutoloadPath); infor.exefilepath = (tmpvalue != null) ? (tmpvalue.ToString()) : ""; myKey.Close(); #endregion }
/// <summary> /// 得到程序自启动的状态 /// </summary> /// <param name="validuser"></param> /// <param name="autologin"></param> /// <param name="Existfile"></param> /// <returns></returns> public static String GetAutoLogonStateName( AutoStartInformation autoStartInfor) { #region AutoLogonSystemState state; _autoStartInfor = autoStartInfor; bool validuser = osUserIsValid(); bool autologin = isAutoLogin(); bool appexeisexist = isExistExeFile(); if (autologin && validuser && appexeisexist) { state = AutoLogonSystemState.isStart; } else if (!autologin) { state = AutoLogonSystemState.notStart; } else if (autologin && (!validuser)) { state = AutoLogonSystemState.Authfaild; } else { state = AutoLogonSystemState.registrationfailed; } return(getAutoLogonState(state)); #endregion }
/// <summary> /// 把所有自动登录并启动程序的信息写入注册表 /// </summary> /// <param name="writeregisterinformation"></param> public static bool WriteAll(AutoStartInformation infor) { #region bool issuccess = false; try { RegistryKey myKeyMachine, myKeyUser; myKeyMachine = Registry.LocalMachine .CreateSubKey(AutoStartInformation.RegistAutoLogonPath); myKeyMachine.SetValue(AutoStartInformation.KeyAutoLogon , getAutoLogonMarkup(infor.AutologonState)); myKeyMachine.SetValue(AutoStartInformation.KeyDefaultUserName , infor.Username); myKeyMachine.SetValue(AutoStartInformation.KeyDefaultPassword , infor.Password); myKeyUser = Registry.CurrentUser.CreateSubKey( AutoStartInformation.RegistAutoLoadExePath); myKeyUser.SetValue(AutoStartInformation.KeyAutoloadPath , infor.exefilepath); myKeyUser.Close(); myKeyMachine.Close(); issuccess = true; } catch (System.Security.SecurityException) { ErrorInformation = "不允许修改注册表,请检查杀毒软件是否关闭,并要用管理员身份运行程序!"; } catch (Exception ex) { ExtConsole.Write(ex.GetType().ToString()); ErrorInformation = "写入注册表失败,请以管理员身份运行程序!"; } return(issuccess); #endregion }