📄 fmmain.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using USBLib;
namespace USBView
{
public partial class fmMain : Form
{
int NodeIndex = 0;
int HubCount = 0;
int DevCount = 0;
StringBuilder sb = new StringBuilder();
Dictionary<string, string> Details;
public fmMain()
{
InitializeComponent();
}
private void fmMain_Load(object sender, EventArgs e)
{
DoIt();
}
private void DoIt()
{
TreeNode HostNode = treeView1.Nodes.Add("My Computer");
HostNode.Tag = NodeIndex++;
Details = new Dictionary<string, string>();
// loop through all of the host controllers
int ControllerCount = 0;
foreach (USB.USBController host in USB.GetHostControllers())
{
TreeNode HubNode = HostNode.Nodes.Add(host.Name);
HubNode.Tag = NodeIndex++;
// put the detailed information into a dictionary
sb.Length = 0;
sb.AppendLine("Index=" + host.Index);
sb.AppendLine("DevicePath=" + host.DevicePath);
sb.AppendLine("DriverKey=" + host.DriverKeyName);
sb.AppendLine("Name=" + host.Name);
Details.Add(HubNode.Tag.ToString(), sb.ToString());
// Get the Root Hub for this USB Controller
USB.USBHub root = host.GetRootHub();
ProcessHub(root, HubNode);
ControllerCount++;
}
// let's fix up the first entry in the Dictionary
Details["0"] = "ControllerCount=" + ControllerCount;
treeView1.ExpandAll();
// Put the count numbers in the status tray
toolStripStatusLabel1.Text = "Devices: " + DevCount + ", Hubs: " + HubCount;
}
//
// a recursive routine to loop thru the nodes
//
private void ProcessHub(USB.USBHub Hub, TreeNode node)
{
// the information about the hub itself
sb.Length = 0;
sb.AppendLine("PortCount=" + Hub.PortCount);
sb.AppendLine("DevicePath=" + Hub.DevicePath);
sb.AppendLine("DriverKey=" + Hub.DriverKey);
sb.AppendLine("Name=" + Hub.Name);
sb.AppendLine("IsBusPowerred=" + Hub.IsBusPowered);
sb.AppendLine("IsRootHub=" + Hub.IsRootHub);
if (!Hub.IsRootHub)
{
sb.AppendLine("InstanceID=" + Hub.InstanceID);
sb.AppendLine("Manufacturer=" + Hub.Manufacturer);
sb.AppendLine("Product=" + Hub.Product);
sb.AppendLine("SerialNumber=" + Hub.SerialNumber);
}
TreeNode HubNode = node.Nodes.Add(Hub.Name);
HubNode.Tag = NodeIndex++;
Details.Add(HubNode.Tag.ToString(), sb.ToString());
// now loop thru all of the down stream ports
foreach (USB.USBPort port in Hub.GetPorts())
{
// information about the port/connection
sb.Length = 0;
sb.AppendLine("HubDevicePath=" + port.HubDevicePath);
sb.AppendLine("PortNumber=" + port.PortNumber);
sb.AppendLine("Status=" + port.Status);
sb.AppendLine("Speed=" + port.Speed);
sb.AppendLine("IsDeviceConnected=" + port.IsDeviceConnected);
sb.AppendLine("IsHub=" + port.IsHub);
TreeNode PortNode = HubNode.Nodes.Add("Port " + port.PortNumber);
PortNode.Tag = NodeIndex++;
Details.Add(PortNode.Tag.ToString(), sb.ToString());
// is this a hub?
if (port.IsHub)
{
USB.USBHub downstreamHub = port.GetHub();
// a Recursive routine...
ProcessHub(downstreamHub, PortNode);
HubCount++;
}
else
{
// display the connected device information
if (port.IsDeviceConnected)
{
USB.USBDevice device = port.GetDevice();
sb.Length = 0;
sb.AppendLine("HubDevicePath=" + device.HubDevicePath);
sb.AppendLine("PortNumber=" + device.PortNumber);
sb.AppendLine("DriverKey=" + device.DriverKey);
sb.AppendLine("InstanceID=" + device.InstanceID);
sb.AppendLine("Name=" + device.Name);
sb.AppendLine("Manufacturer=" + device.Manufacturer);
sb.AppendLine("Product=" + device.Product);
sb.AppendLine("SerialNumber=" + device.SerialNumber);
TreeNode DevNode = PortNode.Nodes.Add(device.Name);
DevNode.Tag = NodeIndex++;
Details.Add(DevNode.Tag.ToString(), sb.ToString());
DevCount++;
}
}
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
// the Details dictionary object was populated while the TreeView
// was being built, so its a simple index to get the data back.
textBox1.Text = Details[e.Node.Tag.ToString()];
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
{
treeView1.Nodes.Clear();
DoIt();
}
// return all connected devices (but not hubs)
private void getDevicesToolStripMenuItem_Click(object sender, EventArgs e)
{
StringBuilder sbDevList = new StringBuilder();
foreach (USB.USBDevice device in USB.GetConnectedDevices())
{
sbDevList.AppendLine("HubDevicePath=" + device.HubDevicePath);
sbDevList.AppendLine("PortNumber=" + device.PortNumber);
sbDevList.AppendLine("DriverKeyName=" + device.DriverKey);
sbDevList.AppendLine("Name=" + device.Name);
sbDevList.AppendLine("InstanceID=" + device.InstanceID);
sbDevList.AppendLine("Manufacturer=" + device.Manufacturer);
sbDevList.AppendLine("Product=" + device.Product);
sbDevList.AppendLine("SerialNumber=" + device.SerialNumber);
sbDevList.AppendLine("");
}
textBox1.Text = sbDevList.ToString();
}
// search for a device by DriverKeyName
private void searchByDriverKeyNameToolStripMenuItem_Click(object sender, EventArgs e)
{
fmSearchKeyName SearchForm = new fmSearchKeyName();
SearchForm.ShowDialog();
if (SearchForm.DriverKeyName != "")
{
USB.USBDevice device = USB.FindDeviceByDriverKeyName(SearchForm.DriverKeyName);
if (device != null)
{
StringBuilder sbDevice = new StringBuilder();
sbDevice.AppendLine("HubDevicePath=" + device.HubDevicePath);
sbDevice.AppendLine("PortNumber=" + device.PortNumber);
sbDevice.AppendLine("DriverKeyName=" + device.DriverKey);
sbDevice.AppendLine("Name=" + device.Name);
sbDevice.AppendLine("InstanceID=" + device.InstanceID);
sbDevice.AppendLine("Manufacturer=" + device.Manufacturer);
sbDevice.AppendLine("Product=" + device.Product);
sbDevice.AppendLine("SerialNumber=" + device.SerialNumber);
textBox1.Text = sbDevice.ToString();
}
else
MessageBox.Show("Could not find a matching device");
}
}
// search for a device by InstanceID
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
fmSearchInstanceID SearchForm = new fmSearchInstanceID();
SearchForm.ShowDialog();
if (SearchForm.InstanceID != "")
{
USB.USBDevice device = USB.FindDeviceByInstanceID(SearchForm.InstanceID);
if (device != null)
{
StringBuilder sbDevice = new StringBuilder();
sbDevice.AppendLine("HubDevicePath=" + device.HubDevicePath);
sbDevice.AppendLine("PortNumber=" + device.PortNumber);
sbDevice.AppendLine("DriverKeyName=" + device.DriverKey);
sbDevice.AppendLine("Name=" + device.Name);
sbDevice.AppendLine("InstanceID=" + device.InstanceID);
sbDevice.AppendLine("Manufacturer=" + device.Manufacturer);
sbDevice.AppendLine("Product=" + device.Product);
sbDevice.AppendLine("SerialNumber=" + device.SerialNumber);
textBox1.Text = sbDevice.ToString();
}
else
MessageBox.Show("Could not find a matching device");
}
}
// search for a device by Drive Letter
private void searchByDriveLetterToolStripMenuItem_Click(object sender, EventArgs e)
{
fmSearchDrive SearchForm = new fmSearchDrive();
SearchForm.ShowDialog();
if (SearchForm.DriveLetter != "")
{
USB.USBDevice device = USB.FindDriveLetter(SearchForm.DriveLetter);
if (device != null)
{
StringBuilder sbDevice = new StringBuilder();
sbDevice.AppendLine("HubDevicePath=" + device.HubDevicePath);
sbDevice.AppendLine("PortNumber=" + device.PortNumber);
sbDevice.AppendLine("DriverKeyName=" + device.DriverKey);
sbDevice.AppendLine("Name=" + device.Name);
sbDevice.AppendLine("InstanceID=" + device.InstanceID);
sbDevice.AppendLine("Manufacturer=" + device.Manufacturer);
sbDevice.AppendLine("Product=" + device.Product);
sbDevice.AppendLine("SerialNumber=" + device.SerialNumber);
textBox1.Text = sbDevice.ToString();
}
else
MessageBox.Show("Could not find a matching device");
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -