使用Visual 2017,将其模板用于.NET Windows窗体应用程序
试图从我的计算机中获取一个usb设备列表,并使用System.Management中的一个名为"GetUSBDevices()“的函数,粗略地将其插入一个学校项目的GetUSBDevices中。
试图使用我所知道的System.Management所具有的函数来完成这个任务,并且在Visual应用程序模板中使用该函数时工作,并且由于某些原因,它不会识别引用.
我试过的:
到目前为止一切都不起作用..。
不起作用的代码:
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;
}
}
}
}给出的错误如下:
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上很好地工作)
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();
}
}
}将非常感谢我能在这件事上得到任何帮助,所以它会成功的!
发布于 2017-03-29 18:08:09
函数GetUSBDevices()不是在System.Management中,而是在您从其他SO question复制的代码中。所以我猜你忘了复制那段代码;-)。
https://stackoverflow.com/questions/43100854
复制相似问题