|
-
Dec 1st, 2011, 06:10 PM
#1
[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?
-
Dec 1st, 2011, 06:31 PM
#2
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
-
Dec 2nd, 2011, 02:36 AM
#3
Re: Viewing a hostfile in textbox
 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
-
Dec 2nd, 2011, 11:49 AM
#4
Re: Viewing a hostfile in textbox
Thanks guys, y'all are the best.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|