Exemple #1
0
        /// <summary>
        /// 设置本地时区
        /// </summary>
        /// <param name="timeZoneName_en"></param>
        /// <returns></returns>
        public static bool SetLocalTimeZone(string timeZoneName_en)
        {
            if (PrivilegeAPI.GrantPrivilege(PrivilegeConstants.SE_TIME_ZONE_NAME))
            {
                DynamicTimeZoneInformation dtzi = timeZoneName2DynamicTimeZoneInformation(timeZoneName_en);
                bool success = false;

                // 检测当前系统是否为旧系统
                if (IsOldOsVersion())
                {
                    Console.WriteLine("检测当前系统为: Old OS Version");
                    TimeZoneInformation tzi = DynamicTimeZoneInformation2TimeZoneInformation(dtzi);
                    success = SetTimeZoneInformation(ref tzi);
                }
                else
                {
                    success = SetDynamicTimeZoneInformation(ref dtzi);
                }

                if (success)
                {
                    TimeZoneInfo.ClearCachedData();  // 清除缓存
                }

                if (!PrivilegeAPI.RevokePrivilege(PrivilegeConstants.SE_TIME_ZONE_NAME))
                {
                    Console.WriteLine("撤权失败: 更改时区");
                }

                return(success);
            }

            Console.WriteLine("授权失败: 更改时区");
            return(false);
        }
Exemple #2
0
        /// <summary>
        /// 设置本地时间
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static bool SetLocalTime(DateTime dateTime)
        {
            if (PrivilegeAPI.GrantPrivilege(PrivilegeConstants.SE_SYSTEMTIME_NAME))
            {
                SystemTime sysTime = DateTime2SystemTime(dateTime);
                bool       success = SetLocalTime(ref sysTime);
                if (!PrivilegeAPI.RevokePrivilege(PrivilegeConstants.SE_SYSTEMTIME_NAME))
                {
                    Console.WriteLine("撤权失败: 更改系统时间");
                }

                return(success);
            }

            Console.WriteLine("授权失败: 更改系统时间");
            return(false);
        }