我正在Microsoft Visual Studio 2014中创建一个程序,以便在我的计算机不再检测到USB时将其锁定。
这是我目前的代码:
Imports System.IO
Public Class Form1
Function USB()
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim list As New List(Of String)
For Each d As DriveInfo In allDrives
If d.IsReady Then
list.Add(d.VolumeLabel)
If Not (list.Contains("NERD STICK")) Then
Label1.Text = "False"
Form2.Show()
Me.BackColor = Color.Red
Else
Label1.Text = "True"
Form2.Hide()
Me.BackColor = Color.Green
End If
End If
Next
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
USB()
End Sub
End Class
Form1只是一个简单的测试,可以看到USB的状态(标签是真还是假,背景为绿色还是红色),而form2是计算机被“锁定”并且USB出来的时候会显示的。
虽然它在某种程度上混淆了,但当计时器滴答并再次运行USB功能时,form2会不断出现和消失。
如果有人能帮忙解决这个问题,我将非常感激!
答案 0 :(得分:1)
您可以重写USB功能并利用LINQ Count运算符
Sub USB()
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim cnt = allDrives.Count(Function(x) x.IsReady AndAlso x.VolumeLabel.Contains("NERD STICK"))
if cnt = 0 then
l.Text = "False"
f.Show()
f.BackColor = Color.Red
else
l.Text = "True"
f.Hide()
f.BackColor = Color.Green
End if
End Sub
这样可以避免对当前代码中检查的每个驱动器进行连续调用Show-Hide (虽然我不确定这会导致闪烁)
作为旁注,我想你知道,如果这是一个简单的保护机制,很容易规避。