软件界面:
下载地址(包含产品及源代码):
微软SkyDrive下载链接:WPFUsbView.zip
源代码:
MainWindow.xaml
<Window x:Class="WPFUsbView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:usb="clr-namespace:WPFUsbView"
Title="USB Device Viewer" Height="600" Width="800" Icon="/WPFUsbView;component/images/usb.ico" WindowStyle="ThreeDBorderWindow" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" WindowState="Maximized">
<Window.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}}}" />
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0" Background="CadetBlue">
<ToolBar Height="40" Width="Auto" HorizontalAlignment="Left" Background="CadetBlue">
<Button Margin="4,0" Name="buttonRefresh" Click="buttonRefresh_Click">
<Image Source="/WPFUsbView;component/images/refresh.png"></Image>
</Button>
<Button Margin="4,0" Name="buttonOpenXML" Click="buttonOpenXML_Click">
<Image Source="/WPFUsbView;component/images/XML.png"></Image>
</Button>
<Button Margin="4,0" Name="buttonInfo" Click="buttonInfo_Click">
<Image Source="/WPFUsbView;component/images/Info.png"></Image>
</Button>
</ToolBar>
</Grid>
<Grid Grid.Row="1" Name="gridDetail">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4*"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="6*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TreeView Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Name="treeView1" FontSize="16" SelectedItemChanged="treeView1_SelectedItemChanged">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type usb:TreeViewUsbItem}" ItemsSource="{Binding Path=Children}">
<StackPanel Orientation="Horizontal">
<Image VerticalAlignment="Center" Source="{Binding Icon}" Width="16" Height="16" Margin="0,0,2,2"></Image>
<TextBlock VerticalAlignment="Center" Text="{Binding Name}"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<GridSplitter Grid.Column="1" Width="13" HorizontalAlignment="Center" VerticalAlignment="Stretch" LayoutUpdated="GridSplitter_LayoutUpdated">
<GridSplitter.Background>
<ImageBrush ImageSource="/WPFUsbView;component/images/SplitLine.png" Stretch="UniformToFill" TileMode="Tile" Viewport="0,0,15,500" ViewportUnits="Absolute" />
</GridSplitter.Background>
</GridSplitter>
<ListView Grid.Column="2" Name="listView1" FontSize="16">
<ListView.ItemTemplate>
<DataTemplate DataType="{x:Type usb:ListViewUsbItem}">
<StackPanel Orientation="Horizontal" Margin="0,2">
<TextBlock Width="250" Text="{Binding Name}"></TextBlock>
<TextBlock Width="Auto" Text="{Binding Value}" Foreground="Blue"></TextBlock>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
<StatusBar Grid.Row="2" Height="32" Background="SteelBlue">
<StatusBarItem>
<Image Source="/WPFUsbView;component/images/usbdevice.png"></Image>
</StatusBarItem>
<StatusBarItem>
<TextBlock FontSize="16" Foreground="Brown" Name="textBlockUsbDevice">0</TextBlock>
</StatusBarItem>
<StatusBarItem>
<Image Source="/WPFUsbView;component/images/usb-hub.png"></Image>
</StatusBarItem>
<StatusBarItem>
<TextBlock FontSize="16" Foreground="Brown" Name="textBlockUsbHub">0</TextBlock>
</StatusBarItem>
</StatusBar>
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.IO;
using System.Windows;
using Splash.IO.PORTS;
using Splash.WPF;
namespace WPFUsbView
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// 枚举设备信息并输出到XML文档
private void buttonOpenXML_Click(object sender, RoutedEventArgs e)
{
String xmlFile = "UsbEnums.xml";
try
{ // 检测当前目录下是否可以创建文件
using (StreamWriter sw = new StreamWriter(xmlFile))
{
sw.Close();
}
}
catch(Exception)
{ // 当前目录无法创建文件,改到我的文档目录下
xmlFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\" + xmlFile;
}
if (USB.EnumUsbToXML(xmlFile))
{ // 判断文件是否存在
if (System.IO.File.Exists(xmlFile))
{ // 打开文件
Splash.Diagnostics.Extensions.ShellExecute(xmlFile);
return;
}
}
MessageBox.Show("Failed!");
return;
}
// 更新设备枚举信息
private void buttonRefresh_Click(object sender, RoutedEventArgs e)
{
// 枚举USB设备信息
treeView1.ItemsSource = TreeViewUsbItem.AllUsbDevices;
// 展开所有分支
treeView1.ExpandAll();
// 设备连接数
textBlockUsbDevice.Text = TreeViewUsbItem.ConnectedDevices.ToString();
// 外部Hub连接数
textBlockUsbHub.Text = TreeViewUsbItem.ConnectedHubs.ToString();
}
// 显示软件版本信息
private void buttonInfo_Click(object sender, RoutedEventArgs e)
{
About AboutWindow = new About();
AboutWindow.Owner = this;
AboutWindow.ShowDialog();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// 显示USB设备枚举信息
buttonRefresh.PerformClick();
}
// 更新布局,调整各控件大小
private void GridSplitter_LayoutUpdated(object sender, EventArgs e)
{
// 设置TreeView的宽度和高度
treeView1.Width = gridDetail.ColumnDefinitions[0].ActualWidth;
treeView1.Height = gridDetail.ActualHeight;
}
private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
TreeViewUsbItem Node = e.NewValue as TreeViewUsbItem;
if (Node != null)
{
listView1.ItemsSource = ListViewUsbItem.UsbDetail(Node.Data);
}
}
}
}