禁用CD驱动器(VB.NET)

时间:2010-02-15 12:34:55

标签: vb.net cd

我有一项任务,不知道如何解决它!

基本上,我想在PC上禁用CD驱动器,这样我们的用户就无法使用它们。

这就是我想要开始的方式 - 最终我想在系统托盘中放置一个图标,允许CD驱动器锁定和解锁,只要你知道密码。

我需要在某个地方开始 - 有人知道如何禁用在VB.net中使用CD驱动器吗?

任何帮助都将不胜感激。

安德鲁

1 个答案:

答案 0 :(得分:1)

我找到了一种方法。

基本上我需要遍历设备管理器中的所有项目,如下所示:

search = New System.Management.ManagementObjectSearcher("SELECT * From Win32_PnPEntity")
            For Each info In search.Get()
                ' Go through each device detected.
            Next

然后我拿了DeviceID和ClassGuid部分。

如果Guid匹配{4D36E965-E325-11CE-BFC1-08002BE10318}这是CD / DVD播放器的GUID,我告诉它根据用户的想法禁用/启用设备。

为了启用或禁用它们,我发现这个方便的程序都准备好了from here

然后我简单地编辑了Form1.vb:

Imports System.Management

Public Class Form1

Private Sub btnEnable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnable.Click
    getCdDrives("Enable")
End Sub

Private Sub btnDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisable.Click
    getCdDrives("Diable")
End Sub

Public Function getCdDrives(ByVal EnableOrDisable As String) As Boolean
    If InputBox("password") = "password" Then
        Try
            Dim info As System.Management.ManagementObject
            Dim search As System.Management.ManagementObjectSearcher
            Dim deviceGuid As String
            Dim deviceType As String
            Dim cameraIsSeenByWindows As Boolean = False
            Dim showDebugPrompts As Boolean = False
            Dim actualGuid As Guid

            search = New System.Management.ManagementObjectSearcher("SELECT * From Win32_PnPEntity")
            For Each info In search.Get()
                ' Go through each device detected.
                deviceType = CType(info("DeviceID"), String)
                deviceGuid = CType(info("ClassGuid"), String)
                If deviceGuid = "{4D36E965-E325-11CE-BFC1-08002BE10318}" Then
                    actualGuid = New Guid(deviceGuid)
                    If EnableOrDisable = "Enable" Then
                        DeviceHelper.SetDeviceEnabled(actualGuid, deviceType, True)
                    Else
                        DeviceHelper.SetDeviceEnabled(actualGuid, deviceType, False)
                    End If
                End If
            Next
            If EnableOrDisable = "Enable" Then
                btnDisable.Enabled = True
                btnEnable.Enabled = False
            Else
                btnDisable.Enabled = False
                btnEnable.Enabled = True
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    Else
        MsgBox("Oooh Va Vu!!")
    End If
End Function

结束班

然后,它将循环通过设备管理器中的CD / DVD驱动器并禁用/启用它们。

我还没有整理代码 - 我需要将脚本作为一个线程运行,因为当它正在做它的时候它会挂起。

我还打算让程序计算出CD驱动器在使用计时器事件时的状态 - 然后相应地报告......然后我需要让它在没有任何形式的系统托盘中运行,最后让它作为启用桌面交互的LSA运行。

当我得到片刻时,我会完成它 - 但你需要的一切都应该在这里。

希望这有点帮助别人!

相关问题