首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows中的System.Management实现无法工作

Windows中的System.Management实现无法工作
EN

Stack Overflow用户
提问于 2017-03-29 18:01:11
回答 1查看 583关注 0票数 0

使用Visual 2017,将其模板用于.NET Windows窗体应用程序

试图从我的计算机中获取一个usb设备列表,并使用System.Management中的一个名为"GetUSBDevices()“的函数,粗略地将其插入一个学校项目的GetUSBDevices中。

试图使用我所知道的System.Management所具有的函数来完成这个任务,并且在Visual应用程序模板中使用该函数时工作,并且由于某些原因,它不会识别引用.

我试过的

  • 通过解决方案资源管理器将System.Management添加为引用
  • 重新安装System.Management引用
  • 直接从库System.Management.GetUSBDevices()调用函数;

到目前为止一切都不起作用..。

不起作用的代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Management; //ISSUE HERE!
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Manager
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void DisplayProcess()
        {
            richTextBox1.Clear();
            Process[] plist = Process.GetProcesses();

            foreach (Process p in plist)
            {
                richTextBox1.Text += "Process " + p.Id.ToString() + " : " + p.ProcessName.ToString() + "\n";
            }

        }
//-----------------------------------ISSUE HERE-----------------------------
//-----------------------------------ISSUE HERE-----------------------------
//-----------------------------------ISSUE HERE-----------------------------
        private void DisplayUSB()
        {
            richTextBox1.Clear();

            var usbDevices = GetUSBDevices();

            foreach (var usb in usbDevices)
            {
                richTextBox1.Text += "USB Device " + usb.DeviceID;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            switch (button1.Text)
            {
                case "Start":
                    button1.Text = "USB";
                    DisplayProcess();

                    break;

                case "USB":
                    button1.Text = "Manager";
                    // DisplayUSB();
                    break;

                case "Manager":
                    DisplayProcess();
                    button1.Text = "USB";
                    break;

                                }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            switch(button1.Text)
            {
                case "Start":
                    MessageBox.Show("Please Start The Manager!","REFRESH ERROR");
                    break;

                case "USB":
                    DisplayProcess();
                    break;

                //case "Manager":
                //    DisplayUSB();
                //    break;

            }
        }
    }
}

给出的错误如下:

代码语言:javascript
复制
Error   CS0103  The name 'GetUSBDevices' does not exist in the current context  Manager c:\users\*****\documents\visual studio 2017\Projects\Manager\Manager\Form1.cs   38  

错误CS1579 foreach语句不能对“?”类型的变量进行操作。因为“?”不包含“GetEnumerator”管理器c:\user*\documents\visual studio 2017\Projects\ Manager \Manager\Form1.cs 40的公共定义

能够工作的代码(取自Get List of connected USB Devices并亲自测试,在我的PC上很好地工作)

代码语言:javascript
复制
namespace ConsoleApplication1
{
  using System;
  using System.Collections.Generic;
  using System.Management;  

  class Program
  {
    static void Main(string[] args)
    {
      var usbDevices = GetUSBDevices();

      foreach (var usbDevice in usbDevices)
      {
        Console.WriteLine("Device ID: {0}, PNP Device ID: {1}, Description: {2}",
            usbDevice.DeviceID, usbDevice.PnpDeviceID, usbDevice.Description);
      }

      Console.Read();
    }

    }
}

将非常感谢我能在这件事上得到任何帮助,所以它会成功的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-29 18:08:09

函数GetUSBDevices()不是在System.Management中,而是在您从其他SO question复制的代码中。所以我猜你忘了复制那段代码;-)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43100854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档