使用autohotkey修改hosts文件而不以管理员

时间:2015-07-09 10:00:02

标签: autohotkey

C:\Windows\System32\drivers\etc中,我有标准的hosts文件

C:\Windows\System32\drivers\etc中,我有一个名为hosts-backup.txt

的文件

使用autohotkey,当我按`abc1 时,我想将hosts文件替换为名为hosts-backup.txt的文件的内容

这是我到目前为止所拥有的:

`::    
    Check := true
    SetTimer, CheckOff, 1000 ; 1 second to type in 001
return

:*:abc1::
    If Check


        ; Read the contents of the backup file 
    FileRead, Contents, C:\Windows\System32\drivers\etc\hosts-backup.txt
    if not ErrorLevel  ; Successfully loaded.
    {
        ListVars
        Pause

        ; delete the old hots file
            FileDelete, C:\Windows\System32\drivers\etc\hosts.txt               
            ; also tried this with C:\Windows\System32\drivers\etc\hosts

        ; save a new copy of the hosts file with the content from the backup file
            FileAppend, %Contents%, C:\Windows\System32\drivers\etc\hosts.txt 
            ; also tried this with C:\Windows\System32\drivers\etc\hosts

            Contents =  ; Free the memory.
    }
return


CheckOff:
    Check := false
return

ListVars返回以下内容:

Global Variables (alphabetical)
--------------------------------------------------
0[1 of 3]: 0
Check[1 of 3]: 0
Contents[0 of 0]:  
ErrorLevel[1 of 3]: 0

运行上述脚本时,C:\Windows\System32\drivers\etc\hosts处的文件不会更新。

我该如何解决这个问题?

根据Blauhirn的建议更新(仍然无效)

`::    
    Check := true
    SetTimer, CheckOff, -1000 ; 1 second to type in abc1
return

:*:abc1::
    If Check{


    ; Read the contents of the backup file 
    FileRead, Contents, C:\Windows\System32\drivers\etc\hosts-backup.txt
    if not ErrorLevel  ; Successfully loaded.
    {
        ListVars

        ; delete the old hots file
            FileDelete, C:\Windows\System32\drivers\etc\hosts.txt    

        ; save a new copy of the hosts file with the content from the backup file
            FileAppend, %Contents%, C:\Windows\System32\drivers\etc\hosts.txt 

            Contents =  ; Free the memory.
    }

     }
return


CheckOff:
    Check := false
return

更新

这似乎是一个权限问题,如果我将脚本编译为exe并以管理员身份运行它,它会按预期工作。

使用autohotkey有一个简单的方法吗?

1 个答案:

答案 0 :(得分:1)

你的这段代码:

<a ng-href="/your/url"></a>

将每1000毫秒重复`:: Check := true SetTimer, CheckOff, 1000 ; 1 second to type in 001 return CheckOff: Check := false return 设置为check。 &#34; Specify a negative Period to indicate that the timer should run only once&#34 ;.由于它不会自动在代码中的任何位置设置回false,因此整个计时器都是无用的。你想用它实现什么? true在前1秒内只会Check。因此,

true

第二次结束后不会做任何事情。另请注意, If Check FileRead, Contents, C:\Windows\System32\drivers\hosts-backup.txt - 子句仅适用于下一行,因为您未使用任何大括号if

修改

我可能错了,但似乎你可以省去计时器并简单地用它来制作一个热字符串:(两个``因为反引号是ahk中的转义字符)

{ }

我认为只有管理员可以编辑system32文件夹..您是否尝试将AutoHotkey.exe更改为&#34;以管理员身份运行&#34;? http://technet.microsoft.com/en-us/magazine/ff431742.aspx