Try Dim StreamReaderHosts As New StreamReader("C:\Windows\System32\Drivers\etc\hosts") Dim StreamReaderListe As New StreamReader("C:\Users\Clément\Documents\ListeDeSite.txt") Dim ligne As String Dim ligneExistante As String = StreamReaderHosts.ReadToEnd StreamReaderHosts.Close() Dim sw As StreamWriter = New StreamWriter("C:\Windows\System32\Drivers\etc\hosts", True) Do ligne = StreamReaderListe.ReadLine 'on verifie si ligne n'est pas vide If ligne = String.Empty Then sw.Close() Exit Do End If If Not ligneExistante.Contains(ligne) Then sw.WriteLine(ligne) End If Loop Until ligne Is Nothing StreamReaderListe.Close() Catch ex As Exception MessageBox.Show(ex.ToString) End Try
Private Sub test() Dim list1, list2 As New List(Of String) Try list1 = File.ReadAllLines("C:\Windows\System32\Drivers\etc\hosts").ToList list2 = File.ReadAllLines("C:\Users\Clément\Documents\ListeDeSite.txt").ToList For Each element In list2 If Not list1.Contains(element) Then list1.Add(element) End If Next File.WriteAllLines("C:\Windows\System32\Drivers\etc\hosts", list1.ToArray) Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub
If Not ligneExistante.Contains(ligne) Then.
Dim StreamReaderHosts As New StreamReader("C:\Windows\System32\Drivers\etc\hosts")
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionDim StreamWriterSautLigne As StreamWriter = New StreamWriter("C:\Windows\System32\Drivers\etc\hosts", True) StreamWriterSautLigne.WriteLine(vbNewLine) StreamWriterSautLigne.Close()ne s'exécute qu'une seule fois juste pour séparer le premier site du
127.0.0.1 localhost
Environment.NewLineet d'après moi, cela résoudrait le problème de 3 sauts de ligne.
Try Dim StreamReaderHosts As New StreamReader("C:\Windows\System32\Drivers\etc\hosts") Dim StreamReaderListe As New StreamReader("C:\Users\Clément\Documents\ListeDeSite.txt") Dim ligne As String Dim ligneExistante As String = StreamReaderHosts.ReadToEnd StreamReaderHosts.Close() Dim StreamWriterSautLigne As StreamWriter = New StreamWriter("C:\Windows\System32\Drivers\etc\hosts", True) StreamWriterSautLigne.WriteLine(vbNewLine) StreamWriterSautLigne.Close() Do ligne = StreamReaderListe.ReadLine 'Dim sw As StreamWriter = New StreamWriter("C:\Windows\System32\Drivers\etc\hosts", True) If Not ligneExistante.Contains(ligne) Then IO.File.AppendAllText("C:\Windows\System32\Drivers\etc\hosts", ligne + Environment.NewLine) 'sw.Close() End If Loop Until ligne Is Nothing StreamReaderListe.Close() Catch ex As Exception MsgBox(ex.ToString) End Try
Seul petit problème, la première ligne ajoutée la toute première fois est sur la même ligne que la dernière ligne du fichier hosts
Private Sub test() Dim list1, list2 As New List(Of String) Try list1 = File.ReadAllLines("C:\Windows\System32\Drivers\etc\hosts").ToList list2 = File.ReadAllLines("C:\Users\Clément\Documents\ListeDeSite.txt").ToList For Each element In list2 If Not list1.Contains(element) Then list1.Add(element) End If Next File.WriteAllLines("C:\Windows\System32\Drivers\etc\hosts", list1.ToArray) Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub
Private Sub test() Dim list1(), list2() As String Try list1 = File.ReadAllLines("C:\Windows\System32\Drivers\etc\hosts") list2 = File.ReadAllLines("C:\Users\Clément\Documents\ListeDeSite.txt") For Each element In list2 If Not list1.Contains(element) Then list1.ToList.Add(element) End If Next File.WriteAllLines("C:\Windows\System32\Drivers\etc\hosts", list1) Catch ex As Exception MessageBox.Show(ex.ToString) End Try End Sub
c'est un problème de Framework, comme j'ai crée mon projet sous le Framework 2.0 pour qu'il marche sur tous les PC
Je l'avais testé sur le Framework 4.5 me semble t-il. Je sais mais au moins je suis sur que le programme marchera sur n'importe quel PC, même si il n'est pas à jour.