[RESOLVED] Viewing a hostfile in textbox
I have a program to add and remove sites from the HOST file. So, sorta like a site blocker. I kinda wish I had a chicken in a police outfit giving the stop! hand sign and name it cock blocker :P anyways... To read what sites/ip's are blocked I'm just reading the HOST file in a read only textbox. I was wanting to know if I can read everything but the crud at the top of the file(Describing how to edit the HOST file manually). How could I do that? Here is my code I'm using to read the HOST file:
Code:
Dim ReadItem As String = String.Empty
IO.File.Exists("c:\windows\system32\drivers\etc\hosts")
ReadItem = IO.File.ReadAllText("c:\windows\system32\drivers\etc\hosts")
TextBox1.Text = ReadItem
TextBox1.ScrollBars = ScrollBars.Both
TextBox1.WordWrap = False
Any suggestions?
Re: Viewing a hostfile in textbox
You could try:
vb Code:
Dim strHostsFile As String = "c:\windows\system32\drivers\etc\hosts"
Dim AllLines() As String
If IO.File.Exists(strHostsFile) Then
AllLines = IO.File.ReadAllLines(strHostsFile)
For Each line As String In AllLines
If Not line.StartsWith("#") AndAlso line <> "" Then TextBox1.AppendText(line & Environment.NewLine)
Next line
End If
Re: Viewing a hostfile in textbox
Quote:
Originally Posted by
Inferrd
You could try:
vb Code:
Dim strHostsFile As String = "c:\windows\system32\drivers\etc\hosts"
Dim AllLines() As String
If IO.File.Exists(strHostsFile) Then
AllLines = IO.File.ReadAllLines(strHostsFile)
For Each line As String In AllLines
If Not line.StartsWith("#") AndAlso line <> "" Then TextBox1.AppendText(line & Environment.NewLine)
Next line
End If
That will ignore commented out hosts entries also.
vb Code:
' Assuming this is host location.
Dim lines() As String = IO.File.ReadAllLines("C:\WINDOWS\system32\drivers\etc\hosts")
Dim n As Integer = 10
While n < lines.Length - 1
Me.RichTextBox1.AppendText(lines(n) & Environment.NewLine)
n += 1
End While
Re: Viewing a hostfile in textbox
Thanks guys, y'all are the best.