使用 WPF 获取你电脑上所有目录的详细信息:
2010-11-26 11:20
190 查看
一:界面设计代码如下:
二:界面展示:
三:后台代码编写:
搞定.................
<Grid> <Border CornerRadius="5" BorderBrush="SteelBlue" BorderThickness="2"> <StackPanel Margin="1,0,-1,0" Width="590"> <Grid Height="443" Name="grid1" Width="595"> <Label FontWeight="Bold" Foreground="White" Margin="1,1,5,406"> <Label.Background> <ImageBrush ImageSource="/MyWpfApplication;component/Image/bg.gif" /> </Label.Background> 图片信息:</Label> <Line Stroke="SteelBlue" StrokeThickness="1" X2="550" Height="12" Margin="18,216,27,215" VerticalAlignment="Center" HorizontalAlignment="Center" /> <Border CornerRadius="5" BorderThickness="2" BorderBrush="SteelBlue" Height="174" HorizontalAlignment="Left" Margin="16,37,0,0" Name="border1" VerticalAlignment="Top" Width="102"> <TreeView Height="164" Name="tvDic" Width="93" SelectedItemChanged="tvDic_SelectedItemChanged" MouseDoubleClick="tvDic_MouseDoubleClick" /> </Border> <Expander Header="附加信息:" Height="221" HorizontalAlignment="Left" Margin="11,225,0,0" Name="expander1" VerticalAlignment="Top" Width="569"> <Grid Width="565" Height="195"> <Border BorderBrush="SteelBlue" BorderThickness="2" CornerRadius="5" Height="135" HorizontalAlignment="Left" Margin="12,16,0,0" Name="border2" VerticalAlignment="Top" Width="530"> <Grid Height="132" Name="grid2" Width="529"> <Grid.ColumnDefinitions> <ColumnDefinition Width="77*" /> <ColumnDefinition Width="170*" /> <ColumnDefinition Width="88*" /> <ColumnDefinition Width="194*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="31*" /> <RowDefinition Height="35*" /> <RowDefinition Height="32*" /> <RowDefinition Height="34*" /> </Grid.RowDefinitions> <TextBlock Height="23" HorizontalAlignment="Center" Margin="11,6,0,0" Name="txtImageId" Text="图像ID:" VerticalAlignment="Top" /> <TextBlock Grid.Row="1" Height="23" HorizontalAlignment="Center" Margin="12,9,0,0" Name="txtChicun" Text="尺寸:" VerticalAlignment="Top" /> <TextBlock Grid.Row="2" Height="23" HorizontalAlignment="Center" Margin="12,6,0,0" Name="txtWhite" Text="宽度:" VerticalAlignment="Top" /> <TextBlock Grid.Row="3" Height="23" HorizontalAlignment="Center" Margin="13,4,0,0" Name="txtHight" Text="高度:" VerticalAlignment="Top" /> <TextBlock Grid.Column="2" Height="23" HorizontalAlignment="Center" Margin="14,6,0,0" Name="txtFbl" Text="水平分辨率:" VerticalAlignment="Top" /> <TextBlock Grid.Column="2" Grid.Row="1" Height="23" HorizontalAlignment="Center" Margin="14,7,0,0" Name="txtcuizhi" Text="垂直分辨率:" VerticalAlignment="Top" /> <TextBlock Grid.Column="2" Grid.Row="2" Height="23" HorizontalAlignment="Center" Margin="17,8,0,0" Name="txtAhtor" Text="作者:" VerticalAlignment="Top" /> <TextBlock Grid.Column="2" Grid.Row="3" Height="23" HorizontalAlignment="Center" Margin="15,7,0,0" Name="txtDate" Text="拍摄日期:" VerticalAlignment="Top" /> <TextBox Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="26,4,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" BorderBrush="SteelBlue" Text="" /> <TextBox BorderBrush="SteelBlue" Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="26,7,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" Text="" /> <TextBox BorderBrush="SteelBlue" Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="26,6,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" Text="" /> <TextBox BorderBrush="SteelBlue" Grid.Column="1" Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="26,5,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" Text="" /> <TextBox BorderBrush="SteelBlue" Grid.Column="3" Height="23" HorizontalAlignment="Left" Margin="34,3,0,0" Name="textBox5" VerticalAlignment="Top" Width="120" Text="" /> <TextBox BorderBrush="SteelBlue" Grid.Column="3" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="34,6,0,0" Name="textBox6" VerticalAlignment="Top" Width="120" Text="" /> <TextBox BorderBrush="SteelBlue" Grid.Column="3" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="34,5,0,0" Name="textBox7" VerticalAlignment="Top" Width="120" Text="" /> <TextBox BorderBrush="SteelBlue" Grid.Column="3" Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="34,5,0,0" Name="textBox8" VerticalAlignment="Top" Width="120" Text="" /> </Grid> </Border> <Button Content="添加" Height="23" HorizontalAlignment="Left" Margin="372,159,0,0" Name="btnResigter" VerticalAlignment="Top" Width="75" Click="btnResigter_Click" Background="SteelBlue" /> <Button Content="重置" Height="23" HorizontalAlignment="Right" Margin="0,159,23,0" Name="btnCancel" VerticalAlignment="Top" Width="75" Click="btnCancel_Click" Background="SteelBlue" /> </Grid> </Expander> <Border BorderBrush="SteelBlue" BorderThickness="2" CornerRadius="5" Name="border3" Margin="125,36,35,232"> <TextBox Height="167" Name="tbProp" Width="425" IsReadOnly="True" /> </Border> </Grid> </StackPanel> </Border> </Grid>
二:界面展示:
三:后台代码编写:
#region 系统自动读出的信息 /// <summary> /// 加载指定目录下的文件和目录 /// </summary> /// <param name="tvi"></param> /// <returns></returns> private bool LoadDicTree(TreeViewItem tvi) { tvi.Items.Clear(); string dicStr = ((string)tvi.Tag).Trim(); if (Directory.Exists(dicStr)) //如果目录存在 { //获取目录信息 DirectoryInfo dicInfo = new DirectoryInfo(dicStr); //遍历目录中所有子目录 foreach (DirectoryInfo item in dicInfo.GetDirectories()) { //如果为系统目录,则不显示到界面 if ((item.Attributes & FileAttributes.System) == FileAttributes.System) continue; //创建TreeViewItem对象 TreeViewItem tviDic = new TreeViewItem(); //保存目录路径到Tag属性 tviDic.Tag = item.FullName; //设置节点显示文本 tviDic.Header = item.Name; //节点添加一个子节点"tmp"作为第一次展开标记 tviDic.Items.Add("tmp"); //添加节点Expanded事件处理函数 tvi.Expanded += new RoutedEventHandler(this.Dic_Expanded); //将节点添加到该父目录下 tvi.Items.Add(tviDic); } //遍历目录中所有子文件 foreach (FileInfo item in dicInfo.GetFiles()) { //创建TreeViewItem对象 TreeViewItem tviFile = new TreeViewItem(); //保存文件路径到Tag属性 tviFile.Tag = item.FullName; //设置节点显示文本 tviFile.Header = item.Name; //将节点添加到父目录下 tvi.Items.Add(tviFile); } return true; } else //目录不存在,不处理 { return false; } } /// <summary> /// 加载系统中逻辑分区信息到目录树 /// </summary> private void LoadLogicDrive() { foreach (string drv in Directory.GetLogicalDrives()) { TreeViewItem tvi = new TreeViewItem(); tvi.Tag = drv; tvi.Header = drv; tvi.Expanded += new RoutedEventHandler(this.Dic_Expanded); tvi.Items.Add("tmp"); this.tvDic.Items.Add(tvi); } } private void tvDic_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { //获取该节点的路径 string dicStr = (string)((TreeViewItem)this.tvDic.SelectedItem).Tag; if (Directory.Exists(dicStr)) //是一个目录 { //显示属性面板 this.tbProp.Visibility = Visibility.Visible; //获取目录信息 DirectoryInfo dicInfo = new DirectoryInfo(dicStr); string hint = ""; //生成目录属性文本 hint += string.Format("目录: {0}/n", "/t"+dicInfo.Name); hint += string.Format("路径 {0}/n", "/t" + dicInfo.FullName); hint += string.Format("根目录: {0}/n", "/t" + dicInfo.Root); hint += string.Format("父目录: {0}/n", "/t" + dicInfo.Parent); hint += string.Format("子目录数: {0}/n", "/t" + dicInfo. GetDirectories().Length); hint += string.Format("文件数: {0}/n", "/t" + dicInfo. GetFiles().Length); hint += string.Format("创建时间: {0}/n", "/t" + dicInfo.CreationTime); hint += string.Format("最近访问时间: {0}/n", "/t" + dicInfo.LastAccessTime); hint += string.Format("最近修改时间: {0}/n", "/t" + dicInfo.LastWriteTime); hint += string.Format("目录属性: {0}/n", "/t" + dicInfo.Attributes); this.tbProp.Text = hint; //显示目录属性到界面 } else if (File.Exists(dicStr)) //是一个文件 { //显示属性面板 this.tbProp.Visibility = Visibility.Visible; //获取文件属性 FileInfo fileInfo = new FileInfo(dicStr); string hint = ""; //生成文件属性文本 hint += string.Format("名称: {0}/n", "/t/t"+fileInfo.Name); hint += string.Format("完整路径: {0}/n","/t"+ fileInfo.FullName); hint += string.Format("扩展名: {0}/n", "/t"+fileInfo.Extension); hint += string.Format("父目录: {0}/n", "/t"+fileInfo.Directory); hint += string.Format("创建时间: {0}/n", "/t"+fileInfo.CreationTime); hint += string.Format("最近访问时间: {0}/n", "/t"+fileInfo.LastAccessTime); hint += string.Format("最近修改时间: {0}/n", "/t"+fileInfo.LastWriteTime); hint += string.Format("文件属性: {0}/n", "/t"+fileInfo.Attributes); this.tbProp.Text = hint; //显示文件属性到界面 } else { //不是文件也不是目录,隐藏属性面板 this.tbProp.Visibility = Visibility.Hidden; } } private void tvDic_MouseDoubleClick(object sender, MouseButtonEventArgs e) { } /// <summary> /// 窗体加载事件处理函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Window_Loaded(object sender, RoutedEventArgs e) { this.tvDic.Items.Clear(); this.LoadLogicDrive(); MybInd(); } /// <summary> /// 节点展开事件处理函数 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void Dic_Expanded(object sender, RoutedEventArgs e) { //获取展开的节点 TreeViewItem tvi = (TreeViewItem)e.Source; if (tvi.Tag == null) //如果节点不带有Tag数据,则返回 { return; } if ((tvi.Items.Count == 1) && //如果节点Tag数据是string类型且为"tmp" (tvi.Items[0] is string) && //则是第一次展开,则加载子 //目录等信息到树形结构中 ((string)tvi.Items[0] == "tmp")) { this.LoadDicTree(tvi); //加载目录的子目录和文件 } } private void btnCancel_Click(object sender, RoutedEventArgs e) { this.textBox1.Text = ""; this.textBox2.Text = ""; this.textBox3.Text = ""; this.textBox4.Text = ""; this.textBox5.Text = ""; this.textBox6.Text = ""; this.textBox7.Text = ""; this.textBox8.Text = ""; } private void btnResigter_Click(object sender, RoutedEventArgs e) { tbProp.Text += txtImageId.Text + "/t/t" + textBox1.Text+"/n"; tbProp.Text += txtChicun.Text + "/t/t" + textBox2.Text+"/n"; tbProp.Text += txtWhite.Text + "/t/t" + textBox3.Text+"/n"; tbProp.Text += txtHight.Text+ "/t/t" + textBox4.Text+"/n"; tbProp.Text += txtFbl.Text + "/t" + textBox5.Text+"/n"; tbProp.Text += txtcuizhi.Text + "/t" + textBox6.Text+"/n"; tbProp.Text += txtAhtor.Text + "/t/t" + textBox7.Text+"/n"; tbProp.Text += txtDate.Text + "/t" + textBox8.Text+"/n"; } #endregion
搞定.................
相关文章推荐
- C#获取IIS所有站点及虚拟目录和应用程序(包含名称及详细信息)
- C#获取IIS所有站点及虚拟目录和应用程序(包含名称及详细信息)
- Qt学习笔记,使用QDir获取当前目录下所有目录和文件信息
- Qt学习笔记,使用QDir获取当前目录下所有目录和文件信息
- 我使用过的Linux命令之ll - 列出文件/目录的详细信息
- 使用python UIAutomation从QQ2017(v8.9)群界面获取所有群成员详细资料,
- 使用组策略获取AD中所有计算机当前登录用户信息
- 使用ShellClass获取文件属性详细信息的实现方法
- Python语言学习讲解七:使用traceback获取详细的异常信息
- Linux获取所有网口及其详细信息
- 使用ttXactAdmin、ttSQLCmdCacheInfo、ttSQLCmdQueryPlan获取SQL相关详细信息[TimesTen运维]
- 使用python获取传入目录下apk文件的所有图标
- windows下获取某个目录下的所有文件信息
- Swift - 使用PhotoKit获取照片1(获取所有照片缩略图、原图及其信息)
- 使用windows API函数获取目录下所有文件名---目录下文件的扫描和处理
- 用js获取电脑信息(是使用与IE浏览器)
- 使用WMI获取远程机器操作系统的详细信息
- 使用ShellClass获取文件属性详细信息
- ☞【JavaSE目录 下的 所有目录 的 详细目录 信息】☜
- multi2sim 编译与使用(2)-详细运行时信息的获取