Foxtable(狐表)用户栏目专家坐堂 → [求助]能否调整电脑系统的时间


  共有168人关注过本帖树形打印复制链接

主题:[求助]能否调整电脑系统的时间

帅哥,在线噢!
有点蓝
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:111394 积分:567040 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/12/10 19:42:00 [显示全部帖子]


 回到顶部
帅哥,在线噢!
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:111394 积分:567040 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2024/12/10 20:12:00 [显示全部帖子]

全局代码

Public Class Form1
    < DllImport("kernel32.dll", SetLastError:=True) > 
    Private Shared Function SetSystemTime(ByRef systime As SYSTEMTIME) As Boolean
    End Function
 
    < StructLayout(LayoutKind.Sequential) > 
    Public Structure SYSTEMTIME
        Public year As Short
        Public month As Short
        Public dayOfWeek As Short
        Public day As Short
        Public hour As Short
        Public minute As Short
        Public second As Short
        Public milliseconds As Short
    End Structure
 
    ' 更改系统时间的函数
    Public Sub ChangeSystemTime(newDateTime As DateTime)
        Dim sysTime As New SYSTEMTIME()
        sysTime.year = CType(newDateTime.Year, Short)
        sysTime.month = CType(newDateTime.Month, Short)
        sysTime.day = CType(newDateTime.Day, Short)
        sysTime.hour = CType(newDateTime.Hour, Short)
        sysTime.minute = CType(newDateTime.Minute, Short)
        sysTime.second = CType(newDateTime.Second, Short)
        sysTime.milliseconds = CType(newDateTime.Millisecond, Short)
 
        If SetSystemTime(sysTime) Then
            ' 成功更改时间
            MessageBox.Show("系统时间已更改。")
        Else
            ' 更改失败,可以通过Marshal.GetLastWin32Error()获取错误代码
            MessageBox.Show("更改系统时间失败,错误代码:" & Marshal.GetLastWin32Error().ToString())
        End If
    End Sub
 

End Class

调用
Dim a As New Form1()
a.ChangeSystemTime(New Date(2024, 12, 9, 12, 0, 0))

 回到顶部