2015-08-18 2 views
0

J'ai créé un programme qui ajoute du texte au fichier hôte system32 et je souhaite qu'un bouton puisse supprimer le fichier sélectionné de la liste et le supprimer de la liste. fichier hôte est le code ici pour l'ajouter ...Comment créer un programme qui supprime les éléments du fichier hôte VB.NET

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 

Merci pour votre temps

+0

Lire toutes les lignes dans un tableau de chaînes. Boucle à travers le tableau quand vous le trouvez l'enlever. Ensuite, écrivez le reste des lignes dans le fichier. De plus, cela ne fonctionnera pas comme vous le pensez. – Codexer

Répondre

1

Vous pouvez utiliser ce code pour ajouter, lire et supprimer les fenêtres fichier hosts.

  1. Ajouter cette classe à votre projet -

WindowsHost Classe:

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. Maintenant, vous pouvez l'utiliser comme ceci

    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