C# 32位程序自动判断当前操作系统是32位/64位版本并更改注册表实现自动登录Windows和开启某个程序
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Win32; //判断操作系统版本(64位\32位)打开注册表项 RegistryKey localKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32); //开机自动进入Windows RegistryKey sub_key0 = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", true); if (this.chk_auto_login_windows.Checked == true) { sub_key0.SetValue("AutoAdminLogon", "1"); sub_key0.SetValue("DefaultDomainName", ""); sub_key0.SetValue("DefaultUserName", txt_auto_login_username.Text); sub_key0.SetValue("DefaultPassword", txt_auto_login_password.Text); sub_key0.SetValue("DontDisplayLastUserName", "1"); sub_key0.SetValue("IgnoreShiftOverride", "1"); foreach (string vname in sub_key0.GetValueNames()) { if (vname == "LegalNoticeCaption") { sub_key0.DeleteValue("LegalNoticeCaption"); } if (vname == "LegalNoticeText") { sub_key0.DeleteValue("LegalNoticeText"); } if (vname == "ForceAutoLogon") { sub_key0.DeleteValue("ForceAutoLogon"); } } } else { foreach (string vname in sub_key0.GetValueNames()) { if (vname == "AutoAdminLogon") { sub_key0.DeleteValue("AutoAdminLogon"); } if (vname == "DefaultDomainName") { sub_key0.DeleteValue("DefaultDomainName"); } if (vname == "DefaultUserName") { sub_key0.DeleteValue("DefaultUserName"); } if (vname == "DefaultPassword") { sub_key0.DeleteValue("DefaultPassword"); } if (vname == "DontDisplayLastUserName") { sub_key0.DeleteValue("DontDisplayLastUserName"); } if (vname == "IgnoreShiftOverride") { sub_key0.DeleteValue("IgnoreShiftOverride"); } if (vname == "LegalNoticeCaption") { sub_key0.DeleteValue("LegalNoticeCaption"); } if (vname == "LegalNoticeText") { sub_key0.DeleteValue("LegalNoticeText"); } if (vname == "ForceAutoLogon") { sub_key0.DeleteValue("ForceAutoLogon"); } } } sub_key0.Close(); //程序随PC启动 RegistryKey sub_key1 = localKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); string Path_str = System.Windows.Forms.Application.ExecutablePath; if (this.checkBox_auto_start.Checked == true) { sub_key1.SetValue("点晴MIS信息通后台服务程序", Path_str); } else { sub_key1.DeleteValue("点晴MIS信息通后台服务程序"); } sub_key1.Close(); localKey.Close();
该文章在 2021/2/18 22:39:00 编辑过 |
关键字查询
相关文章
正在查询... |