在win10系统下,下面代码不能修改系统时间,请大家帮看下

HBJMSHOP 2020-10-04 09:00:45
Imports System.Runtime.InteropServices
Imports System.Text

Public Class SetSystemDateTime

Public Structure SystemTime
Public wYear As UShort
Public wMonth As UShort
Public wDay As UShort
Public wHour As UShort
Public wMinute As UShort
Public wSecond As UShort
End Structure


<DllImport("Kernel32.dll")> _
Public Shared Function SetLocalTime(ByRef sysTime As SystemTime) As Boolean
End Function

Public Function SetLocalTimeByStr(ByVal timestr As String) As Boolean
Dim flag As Boolean = False
Dim sysTime As New SystemTime()
Dim dt As DateTime
Try
'dt = Convert.ToDateTime(timestr)
dt = "2020-10-03 08:10:10"
sysTime.wYear = Convert.ToUInt16(dt.Year)
sysTime.wMonth = Convert.ToUInt16(dt.Month)
sysTime.wDay = Convert.ToUInt16(dt.Day)
sysTime.wHour = Convert.ToUInt16(dt.Hour)
sysTime.wMinute = Convert.ToUInt16(dt.Minute)
sysTime.wSecond = Convert.ToUInt16(dt.Second)
flag = SetSystemDateTime.SetLocalTime(sysTime)
Catch ex As Exception
MsgBox(timestr & " " & ex.Message & Now, 0)
End Try
If Not flag Then MsgBox(dt & " " & sysTime.wDay & " " & flag & " " & Now, 0)
Return flag
End Function

End Class
...全文
17995 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
清晨曦月 2021-01-13
  • 打赏
  • 举报
回复
可能和权限先关
༄邱࿐ 2020-11-23
  • 打赏
  • 举报
回复
引用 楼主 HBJMSHOP 的回复:
Imports System.Runtime.InteropServices Imports System.Text Public Class SetSystemDateTime Public Structure SystemTime Public wYear As UShort Public wMonth As UShort Public wDay As UShort Public wHour As UShort Public wMinute As UShort Public wSecond As UShort End Structure <DllImport("Kernel32.dll")> _ Public Shared Function SetLocalTime(ByRef sysTime As SystemTime) As Boolean End Function Public Function SetLocalTimeByStr(ByVal timestr As String) As Boolean Dim flag As Boolean = False Dim sysTime As New SystemTime() Dim dt As DateTime Try 'dt = Convert.ToDateTime(timestr) dt = "2020-10-03 08:10:10" sysTime.wYear = Convert.ToUInt16(dt.Year) sysTime.wMonth = Convert.ToUInt16(dt.Month) sysTime.wDay = Convert.ToUInt16(dt.Day) sysTime.wHour = Convert.ToUInt16(dt.Hour) sysTime.wMinute = Convert.ToUInt16(dt.Minute) sysTime.wSecond = Convert.ToUInt16(dt.Second) flag = SetSystemDateTime.SetLocalTime(sysTime) Catch ex As Exception MsgBox(timestr & " " & ex.Message & Now, 0) End Try If Not flag Then MsgBox(dt & " " & sysTime.wDay & " " & flag & " " & Now, 0) Return flag End Function End Class
建议你代码统一日期格式,之前吃过一次亏,就是没统一日期格式,导致在Win10或部分Win7出错。现在都是Format一下日期
by_封爱 2020-10-08
  • 打赏
  • 举报
回复
代码我简单看了下.. 跟我的代码是一样的. 基本都是来自网络.. 我们电脑有域控,自己无法更改时间没有管理员权限 没开同步 所以时间慢了一些.. 然后找的代码 c#的 亲测可用..

    public class 设置本地时间
    {
        [DllImport("Kernel32.dll")]
        private static extern bool SetLocalTime(ref SYSTEMTIME lpSystemTime);

        [StructLayout(LayoutKind.Sequential)]
        private 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;
        }

        public static void SetSystemTime(DateTime date)
        {
            SYSTEMTIME lpTime = new SYSTEMTIME();
            lpTime.wYear = Convert.ToUInt16(date.Year);
            lpTime.wMonth = Convert.ToUInt16(date.Month);
            lpTime.wDayOfWeek = Convert.ToUInt16(date.DayOfWeek);
            lpTime.wDay = Convert.ToUInt16(date.Day);
            DateTime time = date;
            lpTime.wHour = Convert.ToUInt16(time.Hour);
            lpTime.wMinute = Convert.ToUInt16(time.Minute);
            lpTime.wSecond = Convert.ToUInt16(time.Second);
            lpTime.wMilliseconds = Convert.ToUInt16(time.Millisecond);
            SetLocalTime(ref lpTime);
        }
    }

基本上 跟你的一模一样.. 我没权限修改时间 用这代码 也一样成功了..
HBJMSHOP 2020-10-07
  • 打赏
  • 举报
回复
大神帮分析下,送分送分
HBJMSHOP 2020-10-05
  • 打赏
  • 举报
回复
WIN7,完全正常
HBJMSHOP 2020-10-05
  • 打赏
  • 举报
回复
没有错误提示, 【flag】这个值为 false
threenewbee 2020-10-04
  • 打赏
  • 举报
回复
MsgBox(timestr & " " & ex.Message & Now, 0)
出现错误提示没有
是不是没有权限

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧