'提前调用
    Imports System.IO
    Imports System.ServiceProcess
     '窗体内 放置 一个RichTextBox 命名为 rtbHost
		
    Private Function Write_Host(ByVal strIP as string,ByVal strHost as string) As String
        Dim strReturn As String = "OK"
        Try
            '读数据
            Dim operachina As String
            Dim strFileName As String

            strFileName = Environ$("SystemRoot") & "\System32\drivers\etc\hosts"

            Dim SR As New StreamReader(strFileName)
            rtbHost.Text = SR.ReadToEnd

            SR.Close()

            If rtbHost.Find(strHost) < 0 Then

                operachina = strIP + "   " + strHost + vbCrLf

                '写数据
                rtbHost.AppendText(vbCrLf + operachina)

                Dim SW As New StreamWriter(strFileName)
                SW.Write(rtbHost.Text)
                SW.Close()

                '重启DNS服务
                Dim service As New ServiceController

                service.ServiceName = "Dnscache"

                If service.Status = ServiceControllerStatus.Running Then

                    service.Stop()
                    service.WaitForStatus(ServiceProcess.ServiceControllerStatus.Stopped)
                    service.Start()
                    service.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running)
                ElseIf service.Status = ServiceControllerStatus.Stopped Then
                    service.Start()
                    service.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running)
                End If

            End If

        Catch ex As Exception
            strReturn = ex.Message
            Throw
        End Try
        Return strReturn
    End Function
    
    '使用方法
     If Write_Host("192.168.1.100","test.wuxing.name") <> "OK" Then MsgBox("DNS解析错误,可能无法访问。")
     System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://test.wuxing.name/")

分类:

程序开发,

最后更新: 5 7 月, 2011

标签:

,