0

I made a program that adds text to the system32 host file and I want a way a button can remove the selected one from the listbox and delete it from the host file here is the code to add it...

 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

Thanks for your time

another user
  • 93
  • 1
  • 11
  • Read all the lines into a string array. Loop through the array when you find it remove it. Then write the rest of the lines back to file. Also this is not going to work like you think it does. – Trevor Aug 19 '15 at 03:02

1 Answers1

3

You can use this code to add, read and delete from windows hosts file.

  1. Add this class to your project -

WindowsHost Class:

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. Now you can use it like this

    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
    
Green Fire
  • 720
  • 1
  • 8
  • 21