KLU3K 发表于 2009-6-30 14:08:01

自己写的代码,哪位能帮我精简一下?全是IF我头晕(只有37行)

本帖最后由 KLU3K 于 2009-7-2 12:28 编辑

很郁闷,想精简一下,不是太会写这种循环,修改Hosts文件的,也是游戏用的,屏蔽连接官网服务器打到提升进入速度的目的。FileSetAttrib(@SystemDir & "\drivers\etc\hosts", "-RSAH")

$HOSTSFilePATCH = @SystemDir & "\drivers\etc\hosts"
$String = FileRead($HOSTSFilePATCH)
$HostFile = "127.0.0.1 localhost"
$CohServ1 = "127.0.0.1 reliclive-1.quazal.net"
$CohServ2 = "127.0.0.1 reliclive.quazal.net"
$CohServ3 = "127.0.0.1 cohlive-1.quazal.net"
$CohServ4 = "127.0.0.1 cohlive.quazal.net"
$HostFile_Comp = StringInStr($String, $HostFile)
$CohServ1_Comp = StringInStr($String, $CohServ1)
$CohServ2_Comp = StringInStr($String, $CohServ2)
$CohServ3_Comp = StringInStr($String, $CohServ3)
$CohServ4_Comp = StringInStr($String, $CohServ4)

If FileExists(@SystemDir & "\drivers\etc\hosts") = 0 Then
        MsgBox(0, "错误:", "Hosts文件不存在,点击确定按钮自动创建。")
EndIf

If $HostFile_Comp = 0 Then
        FileWrite($HOSTSFilePATCH, @CRLF & "127.0.0.1 localhost")
EndIf

If $CohServ1_Comp = 0 Then
        FileWrite($HOSTSFilePATCH, @CRLF & "127.0.0.1 reliclive-1.quazal.net")
EndIf

If $CohServ2_Comp = 0 Then
        FileWrite($HOSTSFilePATCH, @CRLF & "127.0.0.1 reliclive.quazal.net")
EndIf

If $CohServ3_Comp = 0 Then
        FileWrite($HOSTSFilePATCH, @CRLF & "127.0.0.1 cohlive-1.quazal.net")
EndIf

If $CohServ4_Comp = 0 Then
        FileWrite($HOSTSFilePATCH, @CRLF & "127.0.0.1 cohlive.quazal.net")
EndIf
MsgBox(0, "", "修改完毕")

newester 发表于 2009-6-30 15:36:33

直接搞个自解压文件运行覆盖hosts文件更快

都市浪子666 发表于 2009-6-30 15:46:53

用不着这么复杂呀,直接把要 屏蔽的地址添加到Hosts文件就行了嘛
如果有重复的也没关系呀

kn007 发表于 2009-6-30 15:50:16

用bat处理这个最好

maxkingmax 发表于 2009-6-30 15:57:24

用 Selete case case.. endselete 或
Switch case case..endswitch 试试

马甲 发表于 2009-7-1 00:12:56

FileSetAttrib(@SystemDir & "\drivers\etc\hosts", "-RSAH")

$HOSTSFilePATCH = @SystemDir & "\drivers\etc\hosts"
$String = FileRead($HOSTSFilePATCH)

$CohServ = "localhost|reliclive-1.quazal.net|reliclive.quazal.net|cohlive-1.quazal.net|cohlive.quazal.net"
If FileExists(@SystemDir & "\drivers\etc\hosts") = 0 Then
        MsgBox(0, "错误:", "Hosts文件不存在,点击确定按钮自动创建。")
EndIf

$Coh = StringSplit($CohServ,"|",1)
For $i = 1 To $Coh
        If StringInStr($String,$Coh[$i]) = 0 Then FileWrite($HOSTSFilePATCH, @CRLF & "127.0.0.1 "&$Coh[$i])
Next

MsgBox(0, "", "修改完毕")我只会这样子改

jhun 发表于 2009-7-1 01:05:24

本帖最后由 jhun 于 2009-7-1 01:07 编辑

用 Selete case case.. endselete 或
Switch case case..endswitch 试试
maxkingmax 发表于 2009-6-30 15:57 http://www.autoitx.com/images/common/back.gif
case肯定不可以,case只要满足了就不再向下判断你不知道吗?

个人愚见:精简也少不到哪去了,除非条件和后面执行的语句都有规律

mmsivws 发表于 2009-7-1 03:44:19

:face (28):

TheBelief 发表于 2009-7-1 09:14:02

查看下帮助不久好了

59993330 发表于 2009-7-1 09:51:32

建议用DOS 或者用自解压

KLU3K 发表于 2009-7-1 12:39:19

问题基本解决,唯独127.0.0.1 localhost这行无效。

KLU3K 发表于 2009-7-1 12:40:30

非常感谢,问题基本解决,但仍然有个问题是

landays 发表于 2009-7-3 00:04:36

这样如何?FileSetAttrib(@SystemDir & "\drivers\etc\hosts", "-RSAH")
Dim $HOSTSFilePATCH = @SystemDir & "\drivers\etc\hosts" , $HostFile = "127.0.0.1 localhost" , $String = FileRead($HOSTSFilePATCH)
Dim $CohServ1 = "127.0.0.1 reliclive-1.quazal.net" , $CohServ2 = "127.0.0.1 reliclive.quazal.net" , $CohServ3 = "127.0.0.1 cohlive-1.quazal.net" , $CohServ4 = "127.0.0.1 cohlive.quazal.net"
If FileExists(@SystemDir & "\drivers\etc\hosts") = 0 Then MsgBox(0, "错误:", "Hosts文件不存在,点击确定按钮自动创建。")
If StringInStr($String, $HostFile) = 0 Then FileWrite($HOSTSFilePATCH, @CRLF & "127.0.0.1 localhost")
If StringInStr($String, $CohServ1) = 0 Then FileWrite($HOSTSFilePATCH, @CRLF & "127.0.0.1 reliclive-1.quazal.net")
If StringInStr($String, $CohServ2) = 0 Then FileWrite($HOSTSFilePATCH, @CRLF & "127.0.0.1 reliclive.quazal.net")
If StringInStr($String, $CohServ3) = 0 Then FileWrite($HOSTSFilePATCH, @CRLF & "127.0.0.1 cohlive-1.quazal.net")
If StringInStr($String, $CohServ4) = 0 Then FileWrite($HOSTSFilePATCH, @CRLF & "127.0.0.1 cohlive.quazal.net")
MsgBox(0, "", "修改完毕")

caodongchun 发表于 2009-7-3 13:35:21


case肯定不可以,case只要满足了就不再向下判断你不知道吗?

个人愚见:精简也少不到哪去了,除非条件和后面执行的语句都有规律
jhun 发表于 2009-7-1 01:05 http://www.autoitx.com/images/common/back.gif

那就case里加and 或 or 什么的
页: [1]
查看完整版本: 自己写的代码,哪位能帮我精简一下?全是IF我头晕(只有37行)