【问题标题】:How to change system date and time with vb.net on windows 7如何在 Windows 7 上使用 vb.net 更改系统日期和时间
【发布时间】:2023-06-21 03:29:01
【问题描述】:

所以我试图创建一个小程序来快速更改 Windows 7 上的时间。像互联网上的每个人一样,按照“SetSystemTime(ByRef theDateTime As SYSTEMTIME)”我做了同样的事情。

但什么也没发生。

代码如下:

Imports System.Data
Imports System.Text
Imports System.Runtime.InteropServices

Public Class Form1

Private WithEvents timer1 As New Timer

<StructLayoutAttribute(LayoutKind.Sequential)> _
Private 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

<DllImport("kernel32.dll", setLastError:=True)> _
Private Shared Function SetSystemTime(ByRef theDateTime As SYSTEMTIME) As Boolean

End Function

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

    Dim newtime As New SYSTEMTIME

    newtime.year = dtpDate1.Value.Year
    newtime.month = dtpDate1.Value.Year
    newtime.dayOfWeek = dtpDate1.Value.DayOfWeek
    newtime.day = dtpDate1.Value.Day
    newtime.hour = CShort(txtHrs.Text) 'dtpDate1.Value.Hour
    newtime.minute = CShort(txtMins.Text) 'dtpDate1.Value.Minute
    newtime.second = CShort(txtSec.Text) 'dtpDate1.Value.Second
    newtime.milliseconds = dtpDate1.Value.Millisecond

    Try
        If SetSystemTime(newtime) Then
            MsgBox("time changed")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Try


        dtpDate1.Value = DateTime.Now
        dtpDate2.Value = DateTime.Now

        txtHrs.Text = DateTime.Now.Hour
        txtMins.Text = DateTime.Now.Minute
        txtSec.Text = DateTime.Now.Second

        txtHrst.Text = DateTime.Now.Hour
        txtMinst.Text = DateTime.Now.Minute
        txtSect.Text = DateTime.Now.Second

        timer1.Interval = 1000
        timer1.Start()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click

    Dim newtime As New SYSTEMTIME

    newtime.year = dtpDate2.Value.Year
    newtime.month = dtpDate2.Value.Year
    newtime.dayOfWeek = dtpDate2.Value.DayOfWeek
    newtime.day = dtpDate1.Value.Day
    newtime.hour = CShort(txtHrst.Text) 'dtpDate1.Value.Hour
    newtime.minute = CShort(txtMinst.Text) 'dtpDate1.Value.Minute
    newtime.second = CShort(txtSect.Text) 'dtpDate1.Value.Second
    newtime.milliseconds = dtpDate2.Value.Millisecond

    Try
        If SetSystemTime(newtime) Then
            MsgBox("time changed")
        End If
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

End Sub

Private Sub timer1_Tick(sender As Object, e As System.EventArgs) Handles timer1.Tick

    txtSec.Text = CInt(txtSec.Text) + 1
    txtSect.Text = CInt(txtSect.Text) + 1

End Sub

Private Sub txtSec_TextChanged(sender As Object, e As System.EventArgs) Handles txtSec.TextChanged

    If CInt(txtSec.Text) = 60 Then
        txtMins.Text = CInt(txtMins.Text) + 1
        txtSec.Text = 0
    End If

End Sub

Private Sub txtSect_TextChanged(sender As Object, e As System.EventArgs) Handles txtSect.TextChanged

    If CInt(txtSect.Text) = 60 Then
        txtMinst.Text = CInt(txtMinst.Text) + 1
        txtSect.Text = 0
    End If

End Sub


Private Sub txtMins_TextChanged(sender As Object, e As System.EventArgs) Handles txtMins.TextChanged

    If CInt(txtMins.Text) = 60 Then
        txtHrs.Text = CInt(txtHrs.Text) + 1
        txtMins.Text = 0
    End If

End Sub

Private Sub txtMinst_TextChanged(sender As Object, e As System.EventArgs) Handles txtMinst.TextChanged

    If CInt(txtMinst.Text) = 60 Then
        txtHrst.Text = CInt(txtHrst.Text) + 1
        txtMinst.Text = 0
    End If

End Sub

Private Sub txtHrs_TextChanged(sender As Object, e As System.EventArgs) Handles txtHrs.TextChanged

    If CInt(txtHrs.Text) = 24 Then
        dtpDate1.Value.Date.AddDays(1)
        txtHrs.Text = 0
    End If

End Sub

Private Sub txtHrst_TextChanged(sender As Object, e As System.EventArgs) Handles txtHrst.TextChanged

    If CInt(txtHrst.Text) = 24 Then
        dtpDate2.Value.Date.AddDays(1)
        txtHrst.Text = 0
    End If

End Sub
End Class

我不知道自己做错了什么,我尝试构建一个 exe 并以管理员身份运行它。

【问题讨论】:

    标签: .net vb.net datetime elevated-privileges


    【解决方案1】:

    只需使用此代码

    Today = "06/09/2015 06:09:06 PM"
    

    【讨论】:

      【解决方案2】:

      到目前为止,最简单的方法是使用 Microsoft.VisualBasic 命名空间中的内置方法:

          'create a date to use
          Dim d As DateTime
          'set the date to a hour from now
          d = DateTime.Now.AddHours(1)
          'set the system date and time to this date and time - throw an exception if we can't set it
          Try
              Microsoft.VisualBasic.TimeOfDay = d
          Catch ex As Exception
              MessageBox.Show("Could not set the time. You probably need to run as Administrator to do this. " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop)
          End Try
      

      请注意,您通常需要以管理员身份运行才能更改系统日期或时间。

      【讨论】:

      • 那么为什么这仅适用于 admin priv 而我发布的那个甚至不能像 CoDeXeR 先生发布的链接中所说的那样与 admin 一起使用?
      【解决方案3】:

      您可能没有这样做的特权...

      调用进程必须具有 SE_SYSTEMTIME_NAME 权限。默认情况下禁用此权限。 SetLocalTime 函数在更改本地时间之前启用 SE_SYSTEMTIME_NAME 权限,并在返回之前禁用该权限。

      更多关于这里...

      http://msdn.microsoft.com/en-us/library/windows/desktop/ms717802(v=vs.85).aspx

      【讨论】:

      • 好的,有什么解决方法可以让我作为管理员更改日期时间吗?
      • 老实说,只有您链接的部分。我知道更改系统时间和日期似乎不再是管理员的默认权限。现在很“特别”。编辑:我真的不想变得比这更复杂。我为什么要?我是管理员:我应该能够做任何事情。但似乎我需要阅读更多关于该页面中的所有内容。还是谢谢。