在你的代码中,由于 `isHostEntriesAdded` 是一个布尔变量,可能在写入 `hosts` 文件时,没有正确处理其加入与删除操作,从而导致写入了 "False" 这行。不需要将这个布尔值写入到 `hosts` 文件中,因为它并不是有效的主机记录。
### 解决方法
要避免这种情况,你需要确认在添加和删除主机条目时不会意外地将布尔值写入 `hosts` 文件。请确保在 `ButtonAddHost_Click` 方法中的逻辑如以下示例所示。
### 完整代码示例(确保不写入布尔值):
Imports System.IO
Imports System.Security.Principal
Public Class Form1
' 定义要添加的主机规则列表
Private hostEntries As List(Of String) = New List(Of String) From {
"104.18.27.86",
"192.168.1.1",
"example.com" ' 可以根据需要添加其他规则
}
Private hostsPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers\etc\hosts")
Private isHostEntriesAdded As Boolean = False ' 用于跟踪主机规则是否已添加
Private Sub ButtonAddHost_Click(sender As Object, e As EventArgs) Handles ButtonAddHost.Click
' 检查是否是管理员
If Not IsUserAdministrator() Then
MessageBox.Show("请以管理员身份运行此应用程序!", "权限错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
Try
If Not isHostEntriesAdded Then
' 添加每个主机规则
Using writer As New StreamWriter(hostsPath, True)
For Each hostEntry As String In hostEntries
writer.WriteLine(hostEntry)
Next
End Using
MessageBox.Show("已成功添加主机规则!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information)
isHostEntriesAdded = True ' 更新状态为已添加
Else
' 如果已添加,则删除主机规则
Dim lines As List(Of String) = File.ReadAllLines(hostsPath).ToList()
For Each hostEntry As String In hostEntries
lines.RemoveAll(Function(line) line.Trim() = hostEntry) ' 删除主机规则
Next
File.WriteAllLines(hostsPath, lines) ' 重新写入 hosts 文件
MessageBox.Show("已成功删除主机规则!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information)
isHostEntriesAdded = False ' 更新状态为未添加
End If
' 更新现有规则列表
UpdateExistingRules()
Catch ex As Exception
MessageBox.Show("无法修改 hosts 文件:" & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub UpdateExistingRules()
' 清空 ListBox
ListBoxHosts.Items.Clear()
' 读取现有的 hosts 文件内容并填充 ListBox
Try
Dim lines As String() = File.ReadAllLines(hostsPath)
For Each line As String In lines
If Not String.IsNullOrWhiteSpace(line) Then
ListBoxHosts.Items.Add(line) ' 添加非空行到 ListBox
End If
Next
Catch ex As Exception
MessageBox.Show("无法读取 hosts 文件:" & ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Function IsUserAdministrator() As Boolean
Dim identity As WindowsIdentity = WindowsIdentity.GetCurrent()
Dim principal As New WindowsPrincipal(identity)
Return principal.IsInRole(WindowsBuiltInRole.Administrator)
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' 在窗体加载时隐藏 ListBox
ListBoxHosts.Visible = False
UpdateExistingRules() ' 可选:在加载时更新规则
End Sub
Private Sub ButtonToggleListBox_Click(sender As Object, e As EventArgs) Handles ButtonToggleListBox.Click
' 美化按钮
ButtonAddHost.BackColor = Color.FromArgb(30, 144, 255) ' 深天蓝色
ButtonAddHost.ForeColor = Color.White
ButtonAddHost.Font = New Font("Arial", 12, FontStyle.Bold)
ButtonToggleListBox.BackColor = Color.FromArgb(50, 205, 50) ' 较亮绿色
ButtonToggleListBox.ForeColor = Color.White
ButtonToggleListBox.Font = New Font("Arial", 12, FontStyle.Bold)
' 切换 ListBox 的可见性
ListBoxHosts.Visible = Not ListBoxHosts.Visible
' 根据 ListBox 的可见性更新按钮文本
If ListBoxHosts.Visible Then
ButtonToggleListBox.Text = "隐藏当前规则"
Else
ButtonToggleListBox.Text = "显示当前规则"
End If
End Sub
End Class
### 关键点:
1. **确保没有意外写入**:上述代码确保只在需要的地方写入主机条目,并且只写入有效的字符串(IP地址或域名)。
2. **排除布尔值**:`isHostEntriesAdded` 只有在逻辑判断中使用,不会被写入文件。
3. **正常功能**:以上代码应在运行时不会将任何布尔值写入 `hosts` 文件。
### 注意事项:
如果你发现`hosts`文件中仍尝试写入“False”,请确保在其他部分的代码中没有意外地将该变量值写回文件。在测试和运行过程中,确保完整检查逻辑以防止这一问题再次出现。 |