栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

C# 控制电脑本地时间修改

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C# 控制电脑本地时间修改

       这两天项目要做TDOA算法,前提需要时钟同步,甲方客户部署环境是不联网的,需要通过GPS进行更新电脑时间,从网上看了很多方法,但是我这边都不成功,需要管理员权限,问了师父,我是修改了电脑的管理员权限,安全性会有影响,大家仅供参考

 这是方法一:这是调用API文档直接实现的

[StructLayout(LayoutKind.Sequential)]
    public struct Systemtime
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMilliseconds;

        //利用System.DateTime设置SYSTEMTIME数据成员

        public void FromDateTime(DateTime time)
        {
            wYear = (ushort)time.Year;
            wMonth = (ushort)time.Month;
            wDayOfWeek = (ushort)time.DayOfWeek;
            wDay = (ushort)time.Day;
            wHour = (ushort)time.Hour;
            wMinute = (ushort)time.Minute;
            wSecond = (ushort)time.Second;
            wMilliseconds = (ushort)time.Millisecond;
        }

    }

 

 public class SyncServerTime
{
      
        [DllImport("kernel32.dll")]
        public static extern bool SetLocalTime(ref Systemtime time);
        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(ref Systemtime sysTime);
}
static void Main()
{
 Systemtime MySystemTime = new Systemtime();
                        SyncServerTime.GetLocalTime(ref MySystemTime);
                        MySystemTime.FromDateTime(DateTime.Now.AddHours(2));   //在当前时间上加上两个小时
                        Console.WriteLine("修改之前的当前系统时间:{0}", DateTime.Now.ToString());

                        SyncServerTime.SetLocalTime(ref MySystemTime);
                        Console.WriteLine("修改之后的当前系统时间:{0}", DateTime.Now.ToString());
                        Console.ReadKey();

}

方法二 通过调用cmd方法实现

 public class DateTimeHelper
    {
        /// 
        /// 设置本地电脑的年月日
        /// 
        /// 
        /// 
        /// 
        public static void SetLocalDate(int year, int month, int day)
        {
            //实例一个Process类,启动一个独立进程
            Process p = new Process();
            //Process类有一个StartInfo属性
            //设定程序名
            p.StartInfo.FileName = "cmd.exe";
            //设定程式执行参数 “/C”表示执行完命令后马上退出
            p.StartInfo.Arguments = string.Format("/c date {0}-{1}-{2}", year, month, day);
            //关闭Shell的使用
            p.StartInfo.UseShellExecute = false;
            //重定向标准输入
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            //重定向错误输出
            p.StartInfo.RedirectStandardError = true;
            //设置不显示doc窗口
            p.StartInfo.CreateNoWindow = true;
            //启动
            p.Start();
            //从输出流取得命令执行结果
            p.StandardOutput.ReadToEnd();
        }

        /// 
        /// 设置本机电脑的时分秒
        /// 
        /// 
        /// 
        /// 
        public static void SetLocalTime(int hour, int min, int sec)
        {
            //实例一个Process类,启动一个独立进程
            Process p = new Process();
            //Process类有一个StartInfo属性
            //设定程序名
            p.StartInfo.FileName = "cmd.exe";
            //设定程式执行参数 “/C”表示执行完命令后马上退出
            p.StartInfo.Arguments = string.Format("/c time {0}:{1}:{2}", hour, min, sec);
            //关闭Shell的使用
            p.StartInfo.UseShellExecute = false;
            //重定向标准输入
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            //重定向错误输出
            p.StartInfo.RedirectStandardError = true;
            //设置不显示doc窗口
            p.StartInfo.CreateNoWindow = true;
            //启动
            p.Start();
            //从输出流取得命令执行结果
            p.StandardOutput.ReadToEnd();
        }

        /// 
        /// 设置本机电脑的年月日和时分秒
        /// 
        /// 
        public static void SetLocalDateTime(DateTime time)
        {
            Console.WriteLine($"设置本地时间为:{time.ToLongTimeString()}");
            SetLocalDate(time.Year, time.Month, time.Day);
            SetLocalTime(time.Hour, time.Minute, time.Second);
        }
    }

如果需要管理员权限的话 我是这样操作的

第一步:

 

 点击时间 右键选择 调整时间/日期

第二步 吧自动设置时间给关掉

第三步 cmd输入scepol

 按下图操作就可以了

 

 

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/907323.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号