Results 1 to 4 of 4

Thread: [RESOLVED] Viewing a hostfile in textbox

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,394

    Resolved [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?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Viewing a hostfile in textbox

    You could try:
    vb Code:
    1. Dim strHostsFile As String = "c:\windows\system32\drivers\etc\hosts"
    2.  
    3. Dim AllLines() As String
    4. If IO.File.Exists(strHostsFile) Then
    5.     AllLines = IO.File.ReadAllLines(strHostsFile)
    6.     For Each line As String In AllLines
    7.         If Not line.StartsWith("#") AndAlso line <> "" Then TextBox1.AppendText(line & Environment.NewLine)
    8.     Next line
    9. End If

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Viewing a hostfile in textbox

    Quote Originally Posted by Inferrd View Post
    You could try:
    vb Code:
    1. Dim strHostsFile As String = "c:\windows\system32\drivers\etc\hosts"
    2.  
    3. Dim AllLines() As String
    4. If IO.File.Exists(strHostsFile) Then
    5.     AllLines = IO.File.ReadAllLines(strHostsFile)
    6.     For Each line As String In AllLines
    7.         If Not line.StartsWith("#") AndAlso line <> "" Then TextBox1.AppendText(line & Environment.NewLine)
    8.     Next line
    9. End If
    That will ignore commented out hosts entries also.

    vb Code:
    1. ' Assuming this is host location.
    2. Dim lines() As String = IO.File.ReadAllLines("C:\WINDOWS\system32\drivers\etc\hosts")
    3. Dim n As Integer = 10
    4.  
    5. While n < lines.Length - 1
    6.    Me.RichTextBox1.AppendText(lines(n) & Environment.NewLine)
    7.    n += 1
    8. End While

  4. #4

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,394

    Re: Viewing a hostfile in textbox

    Thanks guys, y'all are the best.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width