Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Windows7上用vb.net更改系统日期和时间_.net_Vb.net_Datetime_Elevated Privileges - Fatal编程技术网

如何在Windows7上用vb.net更改系统日期和时间

如何在Windows7上用vb.net更改系统日期和时间,.net,vb.net,datetime,elevated-privileges,.net,Vb.net,Datetime,Elevated Privileges,所以我试图在Windows7上创建一个可以快速改变时间的小程序。按照“SetSystemTime(ByRef theDateTime As SYSTEMTIME)”,就像互联网上的每个人一样,我也这样做了 但什么也没发生 代码如下: Imports System.Data Imports System.Text Imports System.Runtime.InteropServices Public Class Form1 Private WithEvents timer1 As New

所以我试图在Windows7上创建一个可以快速改变时间的小程序。按照“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
导入系统数据
导入系统文本
导入System.Runtime.InteropServices
公开课表格1
Private,事件计时器1作为新计时器
_
私有结构系统时间
公历年短
公众月短
公共星期一
公众假期短
公共时间短
公众分钟
公众第二短
公共毫秒短
端部结构
_
私有共享函数将SYSTEMTIME(ByRef theDateTime作为SYSTEMTIME)设置为布尔值
端函数
私有子按钮1\u单击(发送者作为System.Object,e作为System.EventArgs)处理按钮1。单击
将新时间设置为新系统时间
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.millides=dtpDate1.Value.millides
尝试
如果设置系统时间(newtime),则
MsgBox(“时间更改”)
如果结束
特例
MsgBox(例如消息)
结束尝试
端接头
私有子表单1_Load(发送方作为System.Object,e作为System.EventArgs)处理MyBase.Load
尝试
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
Text=DateTime.Now.Minute
txtssect.Text=DateTime.Now.Second
计时器1.间隔=1000
timer1.Start()
特例
MsgBox(例如消息)
结束尝试
端接头
私有子按钮2\u单击(发送者作为System.Object,e作为System.EventArgs)处理按钮2。单击
将新时间设置为新系统时间
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(txtssect.Text)'dtpDate1.Value.second
newtime.millides=dtpDate2.Value.millides
尝试
如果设置系统时间(newtime),则
MsgBox(“时间更改”)
如果结束
特例
MsgBox(例如消息)
结束尝试
端接头
私有子timer1_Tick(发送方作为对象,e作为System.EventArgs)处理timer1.Tick
txtSec.Text=CInt(txtSec.Text)+1
txtssect.Text=CInt(txtssect.Text)+1
端接头
私有子txtSec_TextChanged(发送方作为对象,e作为System.EventArgs)处理txtSec.TextChanged
如果CInt(txtSec.Text)=60,则
txtMins.Text=CInt(txtMins.Text)+1
txtSec.Text=0
如果结束
端接头
私有子txtssect_TextChanged(发送方作为对象,e作为System.EventArgs)处理txtssect.TextChanged
如果CInt(txtssect.Text)=60,则
txtMinst.Text=CInt(txtMinst.Text)+1
txtssect.Text=0
如果结束
端接头
私有子txtMins_TextChanged(发送方作为对象,e作为System.EventArgs)处理txtMins.TextChanged
如果CInt(txtMins.Text)=60,则
txtHrs.Text=CInt(txtHrs.Text)+1
txtMins.Text=0
如果结束
端接头
私有子txtMinst_TextChanged(发送方作为对象,e作为System.EventArgs)处理txtMinst.TextChanged
如果CInt(txtMinst.Text)=60,则
txtHrst.Text=CInt(txtHrst.Text)+1
txtMinst.Text=0
如果结束
端接头
私有子txtHrs_TextChanged(发送方作为对象,e作为System.EventArgs)处理txtHrs.TextChanged
如果CInt(txtHrs.Text)=24,则
dtpDate1.Value.Date.AddDays(1)
txtHrs.Text=0
如果结束
端接头
私有子txtHrst_TextChanged(发送方作为对象,e作为System.EventArgs)处理txtHrst.TextChanged
如果CInt(txtHrst.Text)=24,则
dtpDate2.Value.Date.AddDays(1)
txtHrst.Text=0
如果结束
端接头
末级

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

您可能没有这样做的特权

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

这里有更多关于这个的


到目前为止,最简单的方法是在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
请注意,您通常需要以管理员身份运行才能更改系统日期或时间。

只需使用此代码即可

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

好的,那么有什么办法可以让我作为管理员更改日期和时间吗?老实说,只有你链接的部分。我明白,更改系统时间和日期似乎不再是管理员的默认权限。现在是“特别”了。编辑:我真的不想变得比这更复杂。我为什么要这样做?我是管理员:我应该可以做任何事情。但我似乎需要更多地阅读该页面中的所有内容。无论如何,谢谢。那么,为什么这只适用于admin priv,而我发布的一个甚至不会像CoDeXeR先生发布的链接中所说的那样适用于admin?