VB修改hosts文件,除掉优酷广告.

2024-12-16 14:34:18
推荐回答(2个)
回答1:

'添加两个文本框+一个按钮,然后粘贴代码如下
Private Sub Command1_Click()
Dim d() As String, t As String
Dim l As Long
Open "c:\WINDOWS\system32\drivers\etc\hosts" For Input As #1
While Not EOF(1)

Line Input #1, t
t = Trim(t)
If Left(t, 1) <> "#" And Len(t) > 0 Then
ReDim Preserve d(0 To l) As String
d(l) = UCase(t)
l = l + 1
End If
Wend
Close #1
For l = 0 To UBound(d)
If InStr(d(l), UCase(Text2.Text)) > 0 Then '这里只检查网址,也可以改成text1.text就是检查IP的
MsgBox "定义有重复" & d(l)
Exit Sub
End If
Next l
Open "c:\WINDOWS\system32\drivers\etc\hosts" For Append As #1
Print #1, Text1.Text & " " & Text2.Text
Close #1
End Sub

Private Sub Form_Load()
Text1.Text = "127.0.0.1"
Text2.Text = "www.baidu.com"
End Sub

ps: text1放IP地址 text2放网址

回答2:

这个去掉youku已经试验成功。只需要画一个按钮Command1就行了

Private Sub Command1_Click()
Dim DATA() As String
Dim I As Integer, fName As String
fName = Environ("windir") & "\system32\drivers\etc\hosts"
Open fName For Binary As #1
ReDim DATA(LOF(1) - 1)
DATA = Split(StrConv(InputB$(LOF(1), 1), 64), vbCrLf)
Close #1
Open fName For Output As #1
For I = 0 To UBound(DATA)
If InStr(1, DATA(I), "youku") < 1 Then Print #1, DATA(I)
Next
Close #1
End Sub