10-16-2011, 04:13 AM
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:
Spoiler (Click to View)
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. */