如何使程序从主机文件VB.NET中删除东西

时间:2015-08-18 21:12:35

标签: vb.net file host system32

我制作了一个程序,将文本添加到system32主机文件中,我想要一种方法,按钮可以从列表框中删除选定的一个,并从主机文件中删除它,这里是添加它的代码......

 If TextBox1.Text = "" Then
        MsgBox("Please Enter A Valid Website Url")

    Else

        path = "C:\Windows\System32\drivers\etc\hosts"
        sw = New StreamWriter(path, True)
        Dim sitetoblock As String = (Environment.NewLine & "127.0.0.1 " & TextBox1.Text) 'has to be www.google.com | NOT: http://www.google.com/
        sw.Write(sitetoblock)
        sw.Close()
        ListBox1.Items.Add(TextBox1.Text)
        MsgBox("Site Blocked")

    End If

感谢您的时间

1 个答案:

答案 0 :(得分:1)

您可以使用此代码在Windows主机文件中添加,读取和删除。

  1. 将此课程添加到您的项目中 -
  2. WindowsHost类:

    Public Class WindowsHost
    Public ReadOnly Location As String
    Private FullMap As List(Of HostMap)
    Public Sub New()
        Location = Environment.SystemDirectory & "\drivers\etc\hosts"
        If Not System.IO.File.Exists(Location) Then
            Throw New System.IO.FileNotFoundException("Host File Was Not Found", Location)
        End If
        FullMap = LoadCurrentMap()
    End Sub
    Public Function Count() As Integer
        Return FullMap.Count
    End Function
    Public Function Item(ByVal index As Integer) As HostMap
        Return New HostMap(FullMap.Item(index))
    End Function
    Public Sub AddHostMap(ByVal NewMap As HostMap)
        FullMap = LoadCurrentMap()
        FullMap.Add(NewMap)
        SaveData()
    End Sub
    Public Sub DeleteHostMapByDomain(ByVal dom As String)
        FullMap = LoadCurrentMap()
        Dim Reall As Integer = 0
        For i As Integer = 0 To FullMap.Count - 1 Step 1
            If FullMap.Item(Reall).domain = dom Then
                FullMap.RemoveAt(Reall)
            Else
                Reall += 1
            End If
        Next
        SaveData()
    End Sub
    Public Sub DeleteHostMapByIp(ByVal ip As System.Net.IPAddress)
        FullMap = LoadCurrentMap()
        Dim Reall As Integer = 0
        For i As Integer = 0 To FullMap.Count - 1 Step 1
            If FullMap.Item(Reall).Ip.Equals(ip) Then
                FullMap.RemoveAt(Reall)
                Reall += 1
            End If
        Next
        SaveData()
    End Sub
    Public Sub UpdateData()
        FullMap = LoadCurrentMap()
    End Sub
    Private Function LoadCurrentMap() As List(Of HostMap)
        Dim FileStream As New System.IO.StreamReader(Location)
        Dim Lines() As String = FileStream.ReadToEnd.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
        FileStream.Close()
        Dim Lst As New List(Of HostMap)
        For Each line As String In Lines
            If Not line.Contains("#") Then
                Dim LineData() As String = line.Split({" "c}, StringSplitOptions.RemoveEmptyEntries)
                If LineData.Length = 2 Then
                    Try
                        Dim temp As New HostMap(LineData(1), System.Net.IPAddress.Parse(LineData(0)))
                        Lst.Add(temp)
                    Catch ex As Exception
                    End Try
                End If
            End If
        Next
        Return Lst
    End Function
    Private Sub SaveData()
        Dim Data As String = "# Windows Host Generate" & vbNewLine & "# Time: " & Now.ToString
        For Each Map As HostMap In FullMap
            Data = Data & vbNewLine & Map.ToString
        Next
        Dim w As New System.IO.StreamWriter(Location)
        w.Write(Data)
        w.Close()
    End Sub
    End Class
    Public Class HostMap
    Public domain As String
    Public Ip As System.Net.IPAddress
    Public Sub New(ByVal _dom As String, ByVal _ip As System.Net.IPAddress)
        domain = _dom
        Ip = _ip
    End Sub
    Public Sub New(ByRef map As HostMap)
        domain = map.domain
        Ip = map.Ip
    End Sub
    Public Overrides Function ToString() As String
        Return Ip.ToString & "       " & domain
    End Function
    End Class
    
    1. 现在您可以像这样使用它

      Dim WindowsHostSession As New WindowsHost()
      WindowsHostSession.AddHostMap(New HostMap("SomeSite.com", System.Net.IPAddress.Parse("127.0.0.1"))) 'Add ip domin map to hosts file
      WindowsHostSession.Item(2).domain 'Read the second ip domain map from the hosts file
      WindowsHostSession.DeleteHostMapByDomain("SomeSite.com") 'Delete all maps with SomeSite.com domain from the hosts file