日期:2014-05-18  浏览次数:20849 次

如何用程序停用设备id对应的系统设备
如题所示,有建设性的指点给高分。

设备ID:在windows设备管理器中选择一个设备后点“属性”在“详细信息”页中的“设备范例id”。







------解决方案--------------------
参考MSDN:
线索1:管理(思路来自dotnet提供的管理接口)
http://msdn.microsoft.com/zh-cn/library/ms184418.aspx
线索2:互操作性(思路来自调用Win32API实现)
http://msdn.microsoft.com/zh-cn/library/ms172270.aspx
线索3:吃饭先,一会儿补充
------解决方案--------------------
接2楼:
http://msdn.microsoft.com/en-us/library/bb905315.aspx
请关注其中的Windows Management Instrumentation这里100%有你要的答案
------解决方案--------------------
你可以参考下这个

启用停用设备 设备管理器 C# API 

http://download.csdn.net/source/352079
------解决方案--------------------
探讨
谢谢楼上的,类似的例子我已经下了好多个了,它们实现的功能是禁用启用同类的所有设备,这样不适合我的需求。我只想启用禁用其中的一个。
有没有高手啊,来帮帮忙啊

------解决方案--------------------
结帖率:0.00% ??怎么混的
------解决方案--------------------
UP
------解决方案--------------------
try:
1.SetupDiGetClassDevs获取设备信息结构句柄
2.SetupDiEnumDeviceInfo遍历每个设备,找出你要停止的(获取每个设备的信息函数很多比如设备范例ID:SetupDiGetDeviceInstanceId)
3.通过SetupDiSetClassInstallParams设置安装的属性
4.调用SetupDiCallClassInstaller应用设备属性
5.最后释放句柄调用:SetupDiDestroyDeviceInfoList

Program.cs
C# code

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace SystemDevices
{
    class Program
    {
        static void Main(string[] args)
        {
            Guid classGuid = Guid.Empty;
            IntPtr hDevInfo = Win32.SetupDiGetClassDevs(ref classGuid, null, IntPtr.Zero, Win32.DIGCF_ALLCLASSES | Win32.DIGCF_PRESENT);
            if (hDevInfo.ToInt32() == Win32.INVALID_HANDLE_VALUE)
            {
                Console.WriteLine("访问硬件设备失败");
            }
            else
            {
                int i = 0;
                StringBuilder deviceName = new StringBuilder();
                deviceName.Capacity = Win32.MAX_DEV_LEN;
                do
                {
                    SP_DEVINFO_DATA devInfoData = new SP_DEVINFO_DATA();
                    devInfoData.cbSize = Marshal.SizeOf(typeof(SP_DEVINFO_DATA));
                    devInfoData.classGuid = Guid.Empty;
                    devInfoData.devInst = 0;
                    devInfoData.reserved = IntPtr.Zero;
                    bool result = Win32.SetupDiEnumDeviceInfo(hDevInfo, i, devInfoData);
                    if (false == result)
                    {
                        break;
                    }
                    Console.WriteLine("Device: {0}", i);
                    Console.WriteLine("\tGuid={0}", devInfoData.classGuid);
                    Console.WriteLine("\tName={0}", Win32.GetClassNameFromGuid(devInfoData.classGuid));
                    Console.WriteLine("\tDescription={0}", Win32.GetClassDescriptionFromGuid(devInfoData.classGuid));
                    Console.WriteLine("\tInstance Id={0}", Win32.GetDeviceInstanceId(hDevInfo, devInfoData));
                    ++i;
                } while (true);
                Console.WriteLine("*****************************************************");
                Console.Write("输入要禁止的设备号(0-{0}):", i-1);
                string str = Console.ReadLine();
                int id;
                if (true == Int32.TryParse(str, out id))
                {
                    if (id >= 0)
                    {
                        Console.Write("输入\"Y\"启用该设备:");
                        str = Console.ReadLine();
                        Console.WriteLine("{0}设备{1}", (str.ToLower() == "y" ? "启用" : "禁用"), Win