Jump to content

HOSTS quick edit


dirty
 Share

Recommended Posts

Edit hosts file and save it locally to ensure you have the latest modified hosts.

NOTE: to FireFox users. Firefox ignoring changes in hosts unless you clear history. its a firefox bug that might never be fixed caz it was reported 100's of times yet no one fixed it yet.

Pretty good thing to add exceptions to your machine, its simple and i love it.

Enjoy.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#RequireAdmin
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)
$MSGTitle = "hosts Quick Edit"
If IsAdmin() Then
StartTheProgram()
Else
    MsgBox (64,$MSGTitle," You need administrator rights to use this feature. Error #" & @ScriptLineNumber)
    Escape()
EndIf



Func StartTheProgram()
EndFunc

$hostsGUI = GUICreate("hosts Quick Editor",200,440,-1,-1,$WS_CAPTION)
GUICtrlCreateLabel ('Example:',0,6,42)
GUICtrlCreateinput ("127.0.0.1 www.google.com",43,3,157)
$Input = GUICtrlCreateEdit ("",0,30,200,370)
$ButtonSave = GUICtrlCreateButton ("Save",0,400,100,40)
GUICtrlSetFont ($ButtonSave,20,1000,'','',10)
$ButtonCancel = GUICtrlCreateButton ("Close",100,400,100,40)
GUICtrlSetFont ($ButtonCancel,20,200,'','',10)

TrayCreateItem('Modify "HOSTS"')
TrayItemSetOnEvent(-1,"ModifyFunction") ;function name
TrayCreateItem('Exit')
TrayItemSetOnEvent(-1,"ExitFunction") ;function name
TraySetState()
ModifyFunction()
;While 1
;empty loop
;
Func ModifyFunction() ;check if file exist localy. If not, copy it to local and only then start reading it and displaying it in edit box
$hosts = (@WindowsDir & "\System32\drivers\etc\hosts")
$checkIfSystemhostsExists = FileExists (@WindowsDir & "\System32\drivers\etc\hosts")
$checkIfLocalhostsExist = FileExists (@ScriptDir & "\Saved\hosts")

    If $checkIfSystemhostsExists = 1 And $checkIfLocalhostsExist = 0 Then
        MsgBox(32,$MSGTitle,"hosts file cant be found in saved folder. Setup will copy system hosts into saved folder. Error #" & @ScriptLineNumber)
    FileCopy ($hosts,@ScriptDir & "\Saved\hosts", 1+8) ;copy system to local
ElseIf $checkIfSystemhostsExists = 1 And $checkIfLocalhostsExist = 1 Then
    FileCopy (@ScriptDir & "\Saved\hosts", $hosts, 1) ;copy local to system
ElseIf $checkIfSystemhostsExists = 0 And $checkIfLocalhostsExist = 1 Then
    MsgBox(32,$MSGTitle,"hosts file cant be found in system directory. Setup will copy saved hosts to system. Error #" & @ScriptLineNumber)
    FileCopy (@ScriptDir & "\Saved\hosts", $hosts, 1) ;copy local to system
ElseIf $checkIfSystemhostsExists = 0 And $checkIfLocalhostsExist = 0 Then
    MsgBox(32,$MSGTitle,"hosts file cant be found. Setup will create new one. Error #" & @ScriptLineNumber)
    FileCopy ($hosts,@ScriptDir & "\Saved\hosts",0+8) ;this will create new local empty file
Else
MsgBox(16,@ScriptName, "Unknown Error")
Escape() ;Go to end of the function is error
EndIf

$OpenFile = FileOpen (@ScriptDir & "\Saved\hosts",0)
If $OpenFile = -1 Then MsgBox (64,@ScriptName,"Error opening \Saved\hosts. Error #" & @ScriptLineNumber)
$fileLocalRead = FileRead ($OpenFile)
GUICtrlSetData ($Input, $fileLocalRead)
GUISetState(@SW_SHOW, $hostsGUI)

While 1
   $msg   = GUIGetMsg()
Switch $msg
;===========================;
Case $GUI_EVENT_CLOSE
    GUIDelete ($hostsGUI)
ExitLoop
;===========================;
Case $ButtonSave
    $Sfile = FileOpen (@ScriptDir & "\Saved\hosts", 2)
    $readAgain = GUICtrlRead ($Input)
    FileClose ($OpenFile)
    FileClose ($Sfile)
    $SfileAgain = FileOpen (@ScriptDir & "\Saved\hosts", 2)
    $write = FileWrite ($SfileAgain, $readAgain)
    FileClose ($SfileAgain)
    $replacehost = FileCopy (@ScriptDir & "\Saved\hosts", @WindowsDir & "\System32\drivers\etc\hosts", 1)
If $write = 0 and $replacehost = 1 Then
    MsgBox(16,$MSGTitle,"Error writing file. Error #" & @ScriptLineNumber)
ElseIf $write = 1 and $replacehost = 0 Then
    MsgBox(16,$MSGTitle,"Error replacing system file. Error #" & @ScriptLineNumber)
ElseIf $write = 0 and $replacehost = 0 Then
    MsgBox(16,$MSGTitle,"Error writing and replacing system file. Error #" & @ScriptLineNumber)
ElseIf $write = 1 and $replacehost = 1 Then
    MsgBox(64,$MSGTitle,"Saved")
Endif
;===========================;
Case $ButtonCancel
    FileClose ($OpenFile)
    GUIDelete ($hostsGUI)
    ExitLoop
;===========================;
    EndSwitch
WEnd

EndFunc

Func ExitFunction()
Exit
EndFunc

Func Escape() ;end of the function
    MsgBox(0,$MSGTitle,'Escaping. Try Again')
EndFunc
Edited by dirty
Link to comment
Share on other sites

  • 9 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...