Jump to content

Recommended Posts

Posted (edited)

Does Autoit not recognize C:\Windows\System32\drivers\etc\hosts as a file and not a directory, or is there some other error going on in my script? I'm trying to toggle the Windows host file on and off  by renaming it on the fly. Also, is it possible to create some kind of persistent visual flag to indicate the host file is on or off, that shuts off when the browser window is closed?

#include <File.au3>
#include <MsgBoxConstants.au3>

$sFile = @SystemDir & "\drivers\etc\host"
If FileExists($sFile) Then
    MsgBox(4160, "Host", "Host file disabled", 3)
    Filemove (@SystemDir & "\drivers\etc\host", @SystemDir & "\drivers\etc\host_old", $FC_OVERWRITE)
    Else
    FileMove(@SystemDir & "\drivers\etc\host_old", @SystemDir & "\drivers\etc\host", $FC_OVERWRITE)
    EndIf

 

Edited by copyleft
Posted

I think it recognizes it properly. I tried this code to get the file size:

$i = FileGetSize(@SystemDir & "\drivers\etc\hosts")
MsgBox(0, "", $i)

And it returned the right file size.  It may be because you have it as host when it should be hosts. Another possible problem is Windows locking on to the file. Try compiling the code, right-click on the executable and go to Run As Administrator.

Posted

@abberration

Yeah, I finally compromised on using the tray tip. It's not persistent and doesn't close with the browser. But it serves the purpose of alerting users. Thanks very much for your help abberration.

#include <File.au3>
#include <MsgBoxConstants.au3>
#include <TrayConstants.au3>

$sFile = @SystemDir & "\drivers\etc\hosts"
If FileExists($sFile) Then
    TrayTip("Hosts", "Host file is not active", 30, $TIP_ICONASTERISK)
MsgBox($MB_SYSTEMMODAL, "", "Click to deactivate host file")
; TrayTip("clears any tray tip", "", 0)
    Filemove (@SystemDir & "\drivers\etc\hosts", @SystemDir & "\drivers\etc\hosts_old", $FC_OVERWRITE)
    Else
    FileMove(@SystemDir & "\drivers\etc\hosts_old", @SystemDir & "\drivers\etc\hosts", $FC_OVERWRITE)
    EndIf

 

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
  • Recently Browsing   0 members

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