Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Host file Modifier - Source
#1
[Image: YzwzC.png]

Here is a simple application which allows you to modify your hostfile. It is multi-threaded and commented. I didn't add any error suppressing as I don't think its needed. If it is I guess I could add it. Anyway here is a screenshot:


Download Source

Code:
'Written by RDCA. Done by request. Fully commented as well.

'NOTE: This will NOT work in the debugger as it requires elevated permissions, to test comple the executable then run
'from the Debug folder.


Imports System.IO
Imports System.Threading



Public Class Form1
    'Declaring a variable that will hold the backup of the original host file.
    Public backup As String


    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Creating a stream reader
        Dim sr As StreamReader = New StreamReader("C:\Windows\System32\drivers\etc\hosts")

        'Setting textbox1's text to the text/value that is read from the StreamReader
        TextBox1.Text = sr.ReadToEnd

        'Closing the stream.
        sr.Close()

        'Setting the value of our backup.
        backup = TextBox1.Text
    End Sub





    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        'Clearing the textbox and setting its new text to the backup.
        TextBox1.Clear()
        TextBox1.Text = backup
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'Clearing textbox1's text and setting the value to the defualt text. (Located in resources)
        TextBox1.Clear()
        TextBox1.Text = My.Resources.String1
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Deleting the file (Requires elevated permissions.
        File.Delete("C:\Windows\System32\drivers\etc\hosts")

        'Declaring our StreamWrriter
        Dim sw As StreamWriter

        'Setting the path of the StreamWriter
        sw = New StreamWriter("C:\Windows\System32\drivers\etc\hosts")

        'Writing our new host file and closing the stream.
        sw.Write(TextBox1.Text)
        sw.Close()
    End Sub

    Private Sub CheckBox1_Click(sender As Object, e As EventArgs) Handles CheckBox1.Click
        'Checking to see if checkbox1 is checked.
        If CheckBox1.Checked = True Then

            'Now we are checking to see if the background worker is already working.
            If Not BackgroundWorker1.IsBusy Then

                'If it's not then we are going to start the background worker.
                BackgroundWorker1.RunWorkerAsync()
            End If
        Else

            'If checkbox1 is not checked then we are going to cancel our background worker
            BackgroundWorker1.CancelAsync()
        End If
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork

        'Checking to make sure checkbox1 is checked.
        While CheckBox1.Checked = True
            'Deleting the hosts file.
            File.Delete("C:\Windows\System32\drivers\etc\hosts")

            'Declaring our Stremwriter.
            Dim sw As StreamWriter

            'Setting the path for the hostfile.
            sw = New StreamWriter("C:\Windows\System32\drivers\etc\hosts")

            'Writing the new hostfile, then closing the stream.
            sw.Write(TextBox1.Text)
            sw.Close()

            'Wating 10 seconds before restarting the check for checkbox1.
            Thread.Sleep(1000)
        End While

    End Sub
End Class

/* Yes I did spell default wrong, to lazy to edit everything and have to re-upload. */
Reply
#2
This is a nice project, this has some good use actually. Thanks for the share, Im gonna take a better look at this in a minute.
~ Digital-Punk
Reply
#3
(10-16-2011, 01:36 PM)Digital-Punk Wrote: This is a nice project, this has some good use actually. Thanks for the share, Im gonna take a better look at this in a minute.

Thanks, and a note to other people. I made this in Visual Studio 2011 so it may not load for you guys. Next time I will make sure to make it in 2011.
Reply
#4
Nice project mate, may come in handy at some point. I'll bookmark this link!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Source ] Hemp Tycoon App [/Source] VB.net KoBE 8 9,355 03-05-2012, 10:30 PM
Last Post: SomeWhiteGuy?
  [Source]Sql Database Project [Source] Digital-Punk 0 1,357 10-16-2011, 07:01 AM
Last Post: Digital-Punk
  [Source]Batch to Exe converter[Source] Digital-Punk 6 2,667 10-15-2011, 03:00 AM
Last Post: Digital-Punk
  [Source]File Assembly changer Vb.Net[Source] Digital-Punk 0 3,018 10-13-2011, 06:35 PM
Last Post: Digital-Punk
  [New\Source\Release] Binary Code <> English Converter [New\Source\Release] Resistance 16 6,982 05-15-2011, 05:42 AM
Last Post: Blixx

Forum Jump:


Users browsing this thread: 1 Guest(s)