Search code examples
c#winformsusbvisual-studio-2017system.management

System.Management Implementation in Windows Form Won't Work


Using Visual Studios RC 2017, using their template for a .NET Windows Form Application

Trying to get a list of usb devices from my computer, and stick it crudely into a rTextbox for a school project, using a function in System.Management called "GetUSBDevices()".

Attempting to do this using a function I know System.Management has, and that works while using in a Visual Studios C# Console Application Template,and for some reason It won't recognize the reference...

I've tried:

  • Adding System.Management as a reference through the solution explorer

  • reinstalling the System.Management Reference

  • calling the function directly from the library [ System.Management.GetUSBDevices(); ]

And nothing has worked so far...

Code That Doesn't Work:

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;

            }
        }
    }
}

Errors given are:

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  

Error CS1579 foreach statement cannot operate on variables of type '?' because '?' does not contain a public definition for 'GetEnumerator' Manager c:\users*****\documents\visual studio 2017\Projects\Manager\Manager\Form1.cs 40

Code That Does Work (taken from Get List of connected USB Devices and tested personally, works on my PC fine)

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();
    }

    }
}

Will very much appreciate any help i can get in the matter, so it will work!


Solution

  • Function GetUSBDevices() is not in System.Management but in the code you copied from the other SO question. So my guess is that you forgot to copy that piece of code ;-).