C# 通过代码开启自动设置时间和自动设置时区选项

自动设置时间,需要看两个值,一个是W32Time下的Parameters里的Type值还有一个是W32Time本身下面的一个Start的值,这两个值设置自动就可以了,单独设置其中一个是不行的必须同时设置。

cs 复制代码
 private void isAutoTimeSetting()
        {
            bool isParametersAuto = false;
            string keyName = @"SYSTEM\CurrentControlSet\Services\W32Time\Parameters";
            using (RegistryKey rKey = Registry.LocalMachine.OpenSubKey(keyName))
            {
                if (rKey != null && rKey.GetValue("Type") != null)
                {
                    string value = rKey.GetValue("Type").ToString();
                    if (value == "NTP")
                    {
                        isParametersAuto = true;
                    }
                    else if (value == "NoSync")
                    {
                        isParametersAuto = false;
                    }
                }
                rKey.Close();
            }

            bool isStartAuto = false;
            string keyName1 = @"SYSTEM\CurrentControlSet\Services\W32Time";
            using (RegistryKey rKey = Registry.LocalMachine.OpenSubKey(keyName1))
            {
                if (rKey != null && rKey.GetValue("Start") != null)
                {
                    string value = rKey.GetValue("Start").ToString();
                    if (value == "3")
                    {
                        isStartAuto = true;
                    }
                    else if (value == "4")
                    {
                        isStartAuto = false;
                    }
                }
                rKey.Close();
            }
            if (isStartAuto && isParametersAuto)
            {
                AppendText(1, $"<Color>已经勾选为自动设置时间</Color>\n", Color.Green);
            }
            else
            {
                string tip = "未勾选为自动设置时间";
                AppendText(1, $"<Color>{tip}</Color>,请检查!\n", Color.Red);
                ErrorAdd(ErrorCode.systemTimeAutoSetting, tip);
            }
        }
相关推荐
OPEN-F1 小时前
Python进阶教程:自动化办公实战
python·c#·自动化
czhc11400756632 小时前
C# 处理体数据:装、钉、交、取消
c#
淡海水2 小时前
03-03-线性-LinkedList-T-源码不变式与选择边界
windows·链表·c#·编译·linkedlist·clr·机器码
longxiaozhang617 小时前
C# Array类:数组其实没那么难
开发语言·c#
longxiaozhang621 小时前
C#运算符重载:让对象也能使用“加减乘除”
开发语言·c#
花落人散处1 天前
C# 核心编程知识点总结:网络编程、委托与事件、异步编程与LINQ
c#
fl1768311 天前
基于C#WPF实现的内存加速球清理类似360加速球内存清理
开发语言·c#·wpf
CoderIsArt1 天前
OPC UA 完整介绍、官网、C# 简单使用
开发语言·c#
界面开发小八哥1 天前
界面控件DevExpress XAF v26.1新版亮点——AI Agent Skills
ai·c#·跨平台·devexpress·ui开发·xaf
csdn_aspnet1 天前
C# 高效便利的处理数据
开发语言·windows·c#