C#:USB设备枚举(八)创建基于WPF的USB设备浏览器

215 篇文章 2 订阅
120 篇文章 3 订阅

作者:Splash

转自:http://blog.csdn.net/jhqin/article/details/6951802


软件界面:

下载地址(包含产品及源代码):

微软SkyDrive下载链接:WPFUsbView.zip

CSDN下载页面:http://download.csdn.net/detail/jhqin/3773593

源代码:

MainWindow.xaml

  1. <Window x:Class="WPFUsbView.MainWindow"  
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  4.         xmlns:usb="clr-namespace:WPFUsbView"          
  5.         Title="USB Device Viewer" Height="600" Width="800" Icon="/WPFUsbView;component/images/usb.ico" WindowStyle="ThreeDBorderWindow" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded" WindowState="Maximized">  
  6.     <Window.Resources>  
  7.         <Style TargetType="TreeViewItem">  
  8.             <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TreeViewItem}}}" />  
  9.         </Style>  
  10.     </Window.Resources>  
  11.   
  12.     <Grid>  
  13.         <Grid.RowDefinitions>  
  14.             <RowDefinition Height="Auto"></RowDefinition>  
  15.             <RowDefinition Height="*"></RowDefinition>  
  16.             <RowDefinition Height="Auto"></RowDefinition>              
  17.         </Grid.RowDefinitions>  
  18.   
  19.         <Grid Grid.Row="0" Background="CadetBlue">  
  20.             <ToolBar Height="40" Width="Auto" HorizontalAlignment="Left" Background="CadetBlue">  
  21.                 <Button Margin="4,0" Name="buttonRefresh" Click="buttonRefresh_Click">  
  22.                     <Image Source="/WPFUsbView;component/images/refresh.png"></Image>  
  23.                 </Button>  
  24.                 <Button Margin="4,0" Name="buttonOpenXML" Click="buttonOpenXML_Click">  
  25.                     <Image Source="/WPFUsbView;component/images/XML.png"></Image>  
  26.                 </Button>  
  27.                 <Button Margin="4,0" Name="buttonInfo" Click="buttonInfo_Click">  
  28.                     <Image Source="/WPFUsbView;component/images/Info.png"></Image>  
  29.                 </Button>  
  30.             </ToolBar>  
  31.         </Grid>     
  32.           
  33.         <Grid Grid.Row="1" Name="gridDetail">  
  34.             <Grid.ColumnDefinitions>  
  35.                 <ColumnDefinition Width="4*"></ColumnDefinition>  
  36.                 <ColumnDefinition Width="Auto"></ColumnDefinition>  
  37.                 <ColumnDefinition Width="6*"></ColumnDefinition>  
  38.             </Grid.ColumnDefinitions>  
  39.   
  40.             <TreeView Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Name="treeView1" FontSize="16" SelectedItemChanged="treeView1_SelectedItemChanged">  
  41.                 <TreeView.ItemTemplate>  
  42.                     <HierarchicalDataTemplate DataType="{x:Type usb:TreeViewUsbItem}" ItemsSource="{Binding Path=Children}">  
  43.                         <StackPanel Orientation="Horizontal">  
  44.                             <Image VerticalAlignment="Center" Source="{Binding Icon}" Width="16" Height="16" Margin="0,0,2,2"></Image>  
  45.                             <TextBlock VerticalAlignment="Center" Text="{Binding Name}"></TextBlock>  
  46.                         </StackPanel>  
  47.                     </HierarchicalDataTemplate>  
  48.                 </TreeView.ItemTemplate>  
  49.             </TreeView>  
  50.   
  51.             <GridSplitter Grid.Column="1" Width="13" HorizontalAlignment="Center" VerticalAlignment="Stretch" LayoutUpdated="GridSplitter_LayoutUpdated">  
  52.                 <GridSplitter.Background>  
  53.                     <ImageBrush ImageSource="/WPFUsbView;component/images/SplitLine.png" Stretch="UniformToFill" TileMode="Tile" Viewport="0,0,15,500" ViewportUnits="Absolute" />  
  54.                 </GridSplitter.Background>  
  55.             </GridSplitter>  
  56.               
  57.             <ListView Grid.Column="2" Name="listView1" FontSize="16">  
  58.                 <ListView.ItemTemplate>  
  59.                     <DataTemplate DataType="{x:Type usb:ListViewUsbItem}">  
  60.                         <StackPanel Orientation="Horizontal" Margin="0,2">  
  61.                             <TextBlock Width="250" Text="{Binding Name}"></TextBlock>  
  62.                             <TextBlock Width="Auto" Text="{Binding Value}" Foreground="Blue"></TextBlock>  
  63.                         </StackPanel>  
  64.                     </DataTemplate>  
  65.                 </ListView.ItemTemplate>  
  66.             </ListView>  
  67.         </Grid>  
  68.   
  69.         <StatusBar Grid.Row="2" Height="32" Background="SteelBlue">  
  70.             <StatusBarItem>  
  71.                 <Image Source="/WPFUsbView;component/images/usbdevice.png"></Image>  
  72.             </StatusBarItem>  
  73.             <StatusBarItem>  
  74.                 <TextBlock FontSize="16" Foreground="Brown" Name="textBlockUsbDevice">0</TextBlock>  
  75.             </StatusBarItem>              
  76.             <StatusBarItem>  
  77.                 <Image Source="/WPFUsbView;component/images/usb-hub.png"></Image>  
  78.             </StatusBarItem>  
  79.             <StatusBarItem>  
  80.                 <TextBlock FontSize="16" Foreground="Brown" Name="textBlockUsbHub">0</TextBlock>  
  81.             </StatusBarItem>  
  82.         </StatusBar>          
  83.     </Grid>  
  84. </Window>  

MainWindow.xaml.cs

[csharp] view plain copy
  1. using System;  
  2. using System.IO;  
  3. using System.Windows;  
  4. using Splash.IO.PORTS;  
  5. using Splash.WPF;  
  6.   
  7. namespace WPFUsbView  
  8. {  
  9.     /// <summary>  
  10.     /// MainWindow.xaml 的交互逻辑  
  11.     /// </summary>  
  12.     public partial class MainWindow : Window  
  13.     {  
  14.         public MainWindow()  
  15.         {  
  16.             InitializeComponent();  
  17.         }  
  18.   
  19.         // 枚举设备信息并输出到XML文档  
  20.         private void buttonOpenXML_Click(object sender, RoutedEventArgs e)  
  21.         {  
  22.             String xmlFile = "UsbEnums.xml";  
  23.             try  
  24.             {   // 检测当前目录下是否可以创建文件  
  25.                 using (StreamWriter sw = new StreamWriter(xmlFile))  
  26.                 {  
  27.                     sw.Close();  
  28.                 }  
  29.             }  
  30.             catch(Exception)  
  31.             {   // 当前目录无法创建文件,改到我的文档目录下  
  32.                 xmlFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\" + xmlFile;  
  33.             }  
  34.   
  35.             if (USB.EnumUsbToXML(xmlFile))  
  36.             {   // 判断文件是否存在  
  37.                 if (System.IO.File.Exists(xmlFile))  
  38.                 {   // 打开文件  
  39.                     Splash.Diagnostics.Extensions.ShellExecute(xmlFile);  
  40.                     return;  
  41.                 }  
  42.             }  
  43.   
  44.             MessageBox.Show("Failed!");  
  45.             return;  
  46.         }  
  47.   
  48.         // 更新设备枚举信息  
  49.         private void buttonRefresh_Click(object sender, RoutedEventArgs e)  
  50.         {  
  51.             // 枚举USB设备信息  
  52.             treeView1.ItemsSource = TreeViewUsbItem.AllUsbDevices;  
  53.   
  54.             // 展开所有分支  
  55.             treeView1.ExpandAll();    
  56.   
  57.             // 设备连接数  
  58.             textBlockUsbDevice.Text = TreeViewUsbItem.ConnectedDevices.ToString();  
  59.   
  60.             // 外部Hub连接数  
  61.             textBlockUsbHub.Text = TreeViewUsbItem.ConnectedHubs.ToString();  
  62.         }  
  63.   
  64.         // 显示软件版本信息  
  65.         private void buttonInfo_Click(object sender, RoutedEventArgs e)  
  66.         {  
  67.             About AboutWindow = new About();  
  68.             AboutWindow.Owner = this;  
  69.             AboutWindow.ShowDialog();  
  70.         }  
  71.   
  72.         private void Window_Loaded(object sender, RoutedEventArgs e)  
  73.         {    
  74.             // 显示USB设备枚举信息  
  75.             buttonRefresh.PerformClick();  
  76.         }  
  77.        
  78.         // 更新布局,调整各控件大小  
  79.         private void GridSplitter_LayoutUpdated(object sender, EventArgs e)  
  80.         {  
  81.             // 设置TreeView的宽度和高度  
  82.             treeView1.Width = gridDetail.ColumnDefinitions[0].ActualWidth;  
  83.             treeView1.Height = gridDetail.ActualHeight;   
  84.         }  
  85.   
  86.         private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)  
  87.         {  
  88.             TreeViewUsbItem Node = e.NewValue as TreeViewUsbItem;  
  89.             if (Node != null)  
  90.             {  
  91.                 listView1.ItemsSource = ListViewUsbItem.UsbDetail(Node.Data);  
  92.             }  
  93.         }  
  94.     }  
  95. }  



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值