Visual Basic时间同步

时间:2018-10-14 17:40:41

标签: vb.net time synchronization

虽然我不是vb.net的专家,但我正在vb.net中从事一个项目,我之所以使用它是因为U认为这是解决此类问题的最佳选择。

我有一个带有两个按钮和一个标签的项目;第一个按钮用于从服务器同步Windows日期,另一个按钮用于将Windows日期更改为(2014,11,16)。之所以这样做,是因为除非日期是这个日期,否则我不会运行某些程序,并且您知道浏览器必须是实时运行的,这才是该项目的想法。

第二个按钮运行正常,但是同步日期按钮不起作用,并在我的标签中抛出此错误

  

无连接,因为目标计算机拒绝连接

这是我的功能和服务器IP

 Public Function GetNISTTime(ByVal host As String) As String

    Dim timeStr As String = ""

    Try
        Dim reader As New StreamReader(New TcpClient(host, 13).GetStream)
        LastSysTime = DateTime.UtcNow()
        timeStr = reader.ReadToEnd()
        reader.Close()
    Catch ex As SocketException
        GetNISTTime = ex.Message
        Exit Function
    Catch ex As Exception
        GetNISTTime = ex.Message
        Exit Function
    End Try

    'Dim jd As Integer = Integer.Parse(timeStr.Substring(1, 5))
    'Dim yr As Integer = Integer.Parse(timeStr.Substring(7, 2))
    'Dim mo As Integer = Integer.Parse(timeStr.Substring(10, 2))
    'Dim dy As Integer = Integer.Parse(timeStr.Substring(13, 2))
    'Dim hr As Integer = Integer.Parse(timeStr.Substring(16, 2))
    'Dim mm As Integer = Integer.Parse(timeStr.Substring(19, 2))
    'Dim sc As Integer = Integer.Parse(timeStr.Substring(22, 2))
    'Dim Temp As Integer = CInt(AscW(timeStr(7)))

    Return timeStr ' New DateTime(yr + 2000, mo, dy, hr, mm, sc)

End Function

和按钮

Private Sub real_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles real.Click
    GetNISTTime("mail.harf.com.sa")
    Label1.Text = GetNISTTime("mail.harf.com.sa").ToString
End Sub

我认为问题是由于服务器造成的,但是我没有找到能成功同步的dns服务器。

如果您想用眼睛看这个问题,这是我的程序下载链接(您应该以管理员身份运行)

http://www.mediafire.com/file/wfw5jpag8w2hofb/Release.rar/file

还必须是沙特阿拉伯时区的dns

2 个答案:

答案 0 :(得分:0)

您的函数调用不正确。

Private Sub real_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles real.Click
    GetNISTTime("mail.harf.com.sa")
    Label1.Text = GetNISTTime("mail.harf.com.sa").ToString
End Sub

应为:

Private Sub real_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles real.Click
    Label1.Text = GetNISTTime("mail.harf.com.sa")
End Sub

GetNISTTime是一个返回字符串的函数,因此,原始的第一行(GetNISTTime("mail.harf.com.sa"))可以完成工作,但返回值却无济于事。原始的第二行使用一个字符串返回值,然后尝试将其转换为字符串。

此外,如果发生错误,您的函数可能不会返回任何内容。您已在catch block中使用了VBA样式分配。相反,请尝试:

Public Function GetNISTTime(ByVal host As String) As String

    Dim timeStr As String = ""

    Try
        Dim reader As New StreamReader(New TcpClient(host, 13).GetStream)
        LastSysTime = DateTime.UtcNow()
        timeStr = reader.ReadToEnd()
        reader.Close()
    Catch ex As SocketException
        return ex.Message
    Catch ex As Exception
        Return ex.Message
    End Try

    'any other stuff
    Return timeStr     
End Function

答案 1 :(得分:0)

所以我确实喜欢Visual Vincent所说的,这是我编辑后的代码 它与我完美搭配,只需要管理员权限 代码

Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Runtime.InteropServices

Public Class Daytime

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim d As DateTime
        d = "12:52:00"

        Try
            Microsoft.VisualBasic.TimeOfDay = d 'Your time...
            Microsoft.VisualBasic.DateString = New Date(2014, 11, 16) 'The date...
        Catch ex As Exception
            'You might have to run as Administrator...?
        End Try
    End Sub

    Private Sub real_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles real.Click
        Process.Start("CMD", "/C net start w32time & w32tm /resync /force & pause")
    End Sub
End Class