根据 C# 中计算的日期时间设置系统时间

本文关键字:时间 设置 系统 日期 计算 根据 | 更新日期: 2023-09-27 18:31:54

我已经复制了代码,我有一个变量("Modifiedtime_1.Modifiedtime")我想用(Modifiedtime_1.Modifiedtime)中可用的日期时间来更改系统时间

if (compare == 0 || compare > 0)
{
    Modifiedtime_1.Modifiedtime = Max.AddMinutes(5);
    Console.WriteLine("----------");
    Console.WriteLine("modifiedtime");
    Console.WriteLine(Modifiedtime_1.Modifiedtime);
    Console.WriteLine("----------");
}

循环被执行。

谢谢。

using System;
using System.Management;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace datetime_1
{
    public class RemoteConnect
    {
        public class Modifiedtime_1
        {
            public static DateTime? Modifiedtime = null;
        }

        public static void Main()
        {
            while (true)
            {
                Console.WriteLine(DateTime.Now.ToString());
                Console.WriteLine("'a");
                Thread.Sleep(1000);
                Console.Clear();
                ConnectionOptions options = new ConnectionOptions();
                options.EnablePrivileges = true;
                options.Impersonation = ImpersonationLevel.Impersonate;
                options.Authentication = AuthenticationLevel.Packet;
                options.Authority = "ntlmdomain:node1.local";
                options.Username = "user1";
                options.Password = "xxxxxx";
                ManagementScope scope = new ManagementScope(string.Format(@"''node1'root'cimv2"), options);
                scope.Connect();
                if (scope.IsConnected == true)
                {
                    Console.WriteLine("node1 Connection Succeeded");
                }
                else
                {
                    Console.WriteLine("node1 Connection Failed");
                }
                ConnectionOptions options1 = new ConnectionOptions();
                options1.EnablePrivileges = true;
                options1.Impersonation = ImpersonationLevel.Impersonate;
                options1.Authentication = AuthenticationLevel.Packet;
                options1.Authority = "ntlmdomain:node2.local";
                options1.Username = "user2";
                options1.Password = "xxxxxx";

                ManagementScope scope1 = new ManagementScope(string.Format(@"''node2'root'cimv2"), options1);
                scope1.Connect();
                if (scope1.IsConnected == true)
                {
                    Console.WriteLine("node2 Connection Succeeded");
                }
                else
                {
                    Console.WriteLine("node2 Connection Failed");
                }
                ConnectionOptions options3 = new ConnectionOptions();
                options3.EnablePrivileges = true;
                options3.Impersonation = ImpersonationLevel.Impersonate;
                options3.Authentication = AuthenticationLevel.Packet;
                options3.Authority = "ntlmdomain:WORKGROUP";
                options3.Username = "user2";
                options3.Password = "xxxx";
                ManagementScope scope3 = new ManagementScope(string.Format(@"''node3'root'cimv2"), options3);
                scope3.Connect();
                if (scope3.IsConnected == true)
                {
                    Console.WriteLine("node3 Connection Succeeded");
                }
                else
                {
                    Console.WriteLine("node3 Connection Failed");
                }
                ConnectionOptions options4 = new ConnectionOptions();
                options4.EnablePrivileges = true;
                options4.Impersonation = ImpersonationLevel.Impersonate;
                options4.Authentication = AuthenticationLevel.Packet;
                options4.Authority = "ntlmdomain:Workgroup";
                options4.Username = "user4";
                options4.Password = "xxxxxx";
                ManagementScope scope4 = new ManagementScope(string.Format(@"''node4'root'cimv2"), options4);
                scope4.Connect();
                if (scope4.IsConnected == true)
                {
                    Console.WriteLine("node4 Connection Succeeded");
                }
                else
                {
                    Console.WriteLine("node4 Connection Failed");
                }
                DateTime Localdatetime = DateTime.Now;
                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_LocalTime");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
                foreach (ManagementObject queryObj in searcher.Get())
                {
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("node1");
                    Console.WriteLine("-----------------------------------");
                    Console.WriteLine("Date: {0}-{1}-{2}", queryObj["Year"], queryObj["Month"], queryObj["Day"]);
                    Console.WriteLine("Time: {0}:{1}:{2}", queryObj["Hour"], queryObj["Minute"], queryObj["Second"]);
                    String node1_Hour_TEMP = queryObj["Hour"].ToString();
                    String node1_Minute_TEMP = queryObj["Minute"].ToString();
                    String node1_SECOND_TEMP = queryObj["Second"].ToString();
                    String node1_Year_TEMP = queryObj["Year"].ToString();
                    String node1_Month_TEMP = queryObj["Month"].ToString();
                    String node1_Day_TEMP = queryObj["Day"].ToString();
                    ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope1, query);
                    foreach (ManagementObject queryObj1 in searcher1.Get())
                    {
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("node2");
                        Console.WriteLine("-----------------------------------");
                        Console.WriteLine("Date: {0}-{1}-{2}", queryObj1["Year"], queryObj1["Month"], queryObj1["Day"]);
                        Console.WriteLine("Time: {0}:{1}:{2}", queryObj1["Hour"], queryObj1["Minute"], queryObj1["Second"]);
                        String node2_Hour_TEMP = queryObj1["Hour"].ToString();
                        String node2_Minute_TEMP = queryObj1["Minute"].ToString();
                        String node2_SECOND_TEMP = queryObj1["Second"].ToString();
                        String node2_Year_TEMP = queryObj1["Year"].ToString();
                        String node2_Month_TEMP = queryObj1["Month"].ToString();
                        String node2_Day_TEMP = queryObj1["Day"].ToString();
                        ManagementObjectSearcher searcher2 =
                            new ManagementObjectSearcher(scope3, query);
                        foreach (ManagementObject queryObj2 in searcher2.Get())
                        {
                            Console.WriteLine("-----------------------------------");
                            Console.WriteLine("node3");
                            Console.WriteLine("-----------------------------------");
                            Console.WriteLine("Date: {0}-{1}-{2}", queryObj2["Year"], queryObj2["Month"], queryObj2["Day"]);
                            Console.WriteLine("Time: {0}:{1}:{2}", queryObj2["Hour"], queryObj2["Minute"], queryObj2["Second"]);
                            String node3_Hour_TEMP = queryObj2["Hour"].ToString();
                            String node3_Minute_TEMP = queryObj2["Minute"].ToString();
                            String node3_SECOND_TEMP = queryObj2["Second"].ToString();
                            String node3_Year_TEMP = queryObj2["Year"].ToString();
                            String node3_Month_TEMP = queryObj2["Month"].ToString();
                            String node3_Day_TEMP = queryObj2["Day"].ToString();

                            ObjectQuery query4 = new ObjectQuery("SELECT * FROM Win32_LocalTime");
                            ManagementObjectSearcher searcher4 = new ManagementObjectSearcher(scope4, query);
                            foreach (ManagementObject queryObj4 in searcher4.Get())
                            {
                                Console.WriteLine("-----------------------------------");
                                Console.WriteLine("node4");
                                Console.WriteLine("-----------------------------------");
                                Console.WriteLine("Date: {0}-{1}-{2}", queryObj4["Year"], queryObj4["Month"], queryObj4["Day"]);
                                Console.WriteLine("Time: {0}:{1}:{2}", queryObj4["Hour"], queryObj4["Minute"], queryObj4["Second"]);
                                String node4_Hour_TEMP = queryObj4["Hour"].ToString();
                                String node4_Minute_TEMP = queryObj4["Minute"].ToString();
                                String node4_SECOND_TEMP = queryObj4["Second"].ToString();
                                String node4_Year_TEMP = queryObj4["Year"].ToString();
                                String node4_Month_TEMP = queryObj4["Month"].ToString();
                                String node4_Day_TEMP = queryObj4["Day"].ToString();
                                //Convert DATE Time String to INT
                                int node1_Hour;
                                int.TryParse(node1_Hour_TEMP, out node1_Hour);
                                int node1_Minute;
                                int.TryParse(node1_Minute_TEMP, out node1_Minute);
                                int node1_SECOND;
                                int.TryParse(node1_SECOND_TEMP, out node1_SECOND);
                                int node1_YEAR;
                                int.TryParse(node1_Year_TEMP, out node1_YEAR);
                                int node1_MONTH;
                                int.TryParse(node1_Month_TEMP, out node1_MONTH);
                                int node1_DAY;
                                int.TryParse(node1_Day_TEMP, out node1_DAY);
                                DateTime node1_DATE = new DateTime(node1_YEAR, node1_MONTH, node1_DAY, node1_Hour, node1_Minute, node1_SECOND);
                                //Convert DATE Time String to INT
                                int node2_Hour;
                                int.TryParse(node2_Hour_TEMP, out node2_Hour);
                                int node2_Minute;
                                int.TryParse(node2_Minute_TEMP, out node2_Minute);
                                int node2_SECOND;
                                int.TryParse(node2_SECOND_TEMP, out node2_SECOND);
                                int node2_YEAR;
                                int.TryParse(node2_Year_TEMP, out node2_YEAR);
                                int node2_MONTH;
                                int.TryParse(node2_Month_TEMP, out node2_MONTH);
                                int node2_DAY;
                                int.TryParse(node2_Day_TEMP, out node2_DAY);
                                DateTime node2_DATE = new DateTime(node2_YEAR, node2_MONTH, node2_DAY, node2_Hour, node2_Minute, node2_SECOND);
                                //Convert DATE Time String to INT
                                int node3_Hour;
                                int.TryParse(node3_Hour_TEMP, out node3_Hour);
                                int node3_Minute;
                                int.TryParse(node3_Minute_TEMP, out node3_Minute);
                                int node3_SECOND;
                                int.TryParse(node3_SECOND_TEMP, out node3_SECOND);
                                int node3_YEAR;
                                int.TryParse(node3_Year_TEMP, out node3_YEAR);
                                int node3_MONTH;
                                int.TryParse(node3_Month_TEMP, out node3_MONTH);
                                int node3_DAY;
                                int.TryParse(node3_Day_TEMP, out node3_DAY);
                                DateTime node3_DATE = new DateTime(node3_YEAR, node3_MONTH, node3_DAY, node3_Hour, node3_Minute, node3_SECOND);
                                //Convert DATE Time String to INT
                                int node4_Hour;
                                int.TryParse(node4_Hour_TEMP, out node4_Hour);
                                int node4_Minute;
                                int.TryParse(node4_Minute_TEMP, out node4_Minute);
                                int node4_SECOND;
                                int.TryParse(node4_SECOND_TEMP, out node4_SECOND);
                                int node4_YEAR;
                                int.TryParse(node4_Year_TEMP, out node4_YEAR);
                                int node4_MONTH;
                                int.TryParse(node4_Month_TEMP, out node4_MONTH);
                                int node4_DAY;
                                int.TryParse(node4_Day_TEMP, out node4_DAY);
                                DateTime node4_DATE = new DateTime(node4_YEAR, node4_MONTH, node4_DAY, node4_Hour, node4_Minute, node4_SECOND);
                                Console.WriteLine("LocalDateTime");
                                Console.WriteLine(Localdatetime);
                                Console.WriteLine("=====LINQ=====");
                                var Max = new[] { node1_DATE, node2_DATE, node3_DATE, node4_DATE }.Max();
                                Console.WriteLine(Max);
                                Console.WriteLine("=====LINQEND=====");

                                //compare datetime with local.
                                int compare = DateTime.Compare(Max, Localdatetime);
                                if (compare == 0 || compare > 0)
                                {
                                    Modifiedtime_1.Modifiedtime = Max.AddMinutes(5);
                                    Console.WriteLine("----------");
                                    Console.WriteLine("modifiedtime");
                                    Console.WriteLine(Modifiedtime_1.Modifiedtime);
                                    Console.WriteLine("----------");
                                }
                                else
                                {
                                    Modifiedtime_1.Modifiedtime = Localdatetime;
                                    Console.WriteLine("----------");
                                    Console.WriteLine("modifiedtime");
                                    Console.WriteLine(Modifiedtime_1.Modifiedtime);
                                    Console.WriteLine("----------");
                                }
                            }
                        }
                    }
            }
        }
    }
}
}

根据 C# 中计算的日期时间设置系统时间

为什么不做这样的事情

在你的无限循环之前。

bool firstPass = true;

在您的循环内

... Work out what Modifiedtime_1.Modifiedtime value is.

在循环的尽头

if ( firstPass || (..times deviated to much calculation)) 
{
    firstPass = false;
    SetLocalMachineTimeMethod(Modifiedtime_1.Modifiedtime);
}

其中 SetLocalMachineTimeMethod 是您调用的一些例程,用于在本地设置时间。还可以增加检查修改时间和(本地系统时间)的改进,以查看它们是否偏离了太多,然后允许再次进行调用。