Search code examples
vb.nettestingblocking

Needing VB to read from a txt file or any other file and do a specific action


So I am building a blocker program for my own family's personal use.

So lets say I have a txt file listing these websites: www.notarealwebsite.com www.fake.net

I want it to find the txt file that has that information, then pull it and use it and since my program is a web browser I want it to block those websites I defined.

I ONLY WANT IT TO BLOCK THE WEBSITE IN MY APPLICATION NOT OVER THE WHOLE COMPUTER (meaning I don't want it to be blocked in Chrome, Firefox, Internet Explorer, Safari, etc...)


Solution

  • "blocked.txt": One site per line.

    It's not ready to be used directly, because it can be easily bypassed by entering "somesite.com/" instead of the blocked "somesite.com".

    But it should give you the idea.

        Dim urlBlocked As Boolean = False ''The ugly way...
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each line As String In System.IO.File.ReadAllLines("blocked.txt")
            If TextBox1.Text = line Then
                urlBlocked = True
                MsgBox("Action Blocked!")
            Else
                If Not urlBlocked Then
                    WebBrowser1.Navigate(TextBox1.Text)
                End If
            End If
        Next
        urlBlocked = False
    End Sub