第九章,小型资源管理器
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第九章,小型资源管理器相关的知识,希望对你有一定的参考价值。
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace SmallResourceManager 8 { 9 public class MyFile 10 { 11 //文件名 12 public string Name { get;set; } 13 //文件类型 14 public string Type { get; set; } 15 //文件大小 16 public double FileSize { get; set; } 17 //文件路径 18 public string Path { get; set; } 19 } 20 }
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.IO; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 12 namespace SmallResourceManager 13 { 14 public partial class FrmMian : Form 15 { 16 public FrmMian() 17 { 18 InitializeComponent(); 19 } 20 List<MyFile> myFile = new List<MyFile>(); 21 public void BindFile() 22 { 23 try 24 { 25 this.lvResource.Items.Clear(); 26 DirectoryInfo di = new DirectoryInfo(this.tvResourceList.SelectedNode.Tag.ToString()); 27 FileInfo[] dirs = di.GetFiles(); 28 29 foreach (FileInfo item in dirs) 30 { 31 MyFile my = new MyFile(); 32 my.FileSize = item.Length; 33 my.Name = item.Name; 34 my.Path = item.FullName; 35 my.Type = item.Extension; 36 myFile.Add(my); 37 ListViewItem lv = new ListViewItem(item.Name); 38 //lv.Text = item.Name; 39 lv.SubItems.Add(item.Length.ToString()); 40 lv.SubItems.Add(item.Extension); 41 lv.SubItems.Add(item.FullName); 42 this.lvResource.Items.Add(lv); 43 } 44 45 } 46 catch (Exception) 47 { 48 49 } 50 } 51 /// <summary> 52 /// 初始TreeView 53 /// </summary> 54 public void IniTreeView() 55 { 56 TreeNode tn = new TreeNode("D:/"); 57 tn.Tag = "D:/"; 58 this.tvResourceList.Nodes.Add(tn); 59 DirectoryInfo info = new DirectoryInfo(this.tvResourceList.Nodes[0].Tag.ToString()); 60 DirectoryInfo[] dirs = info.GetDirectories(); 61 foreach (var item in dirs) 62 { 63 TreeNode node = new TreeNode(item.ToString()); 64 node.Tag = item.FullName; 65 this.tvResourceList.Nodes[0].Nodes.Add(node); 66 } 67 //展开所有树形节点 68 this.tvResourceList.ExpandAll(); 69 70 } 71 /// <summary> 72 /// 加载资源节点 73 /// </summary> 74 /// <param name="sender"></param> 75 /// <param name="e"></param> 76 private void FrmMian_Load(object sender, EventArgs e) 77 { 78 IniTreeView(); 79 } 80 /// <summary> 81 /// 选中节点后绑定listView 82 /// </summary> 83 /// <param name="sender"></param> 84 /// <param name="e"></param> 85 private void tvResourceList_AfterSelect(object sender, TreeViewEventArgs e) 86 { 87 BindFile(); 88 } 89 /// <summary> 90 /// 复制 91 /// </summary> 92 /// <param name="sender"></param> 93 /// <param name="e"></param> 94 private void tsmiCopy_Click(object sender, EventArgs e) 95 { 96 if (lvResource.SelectedItems.Count > 0) 97 { 98 FolderBrowserDialog fbd = new FolderBrowserDialog(); 99 DialogResult dresult = fbd.ShowDialog(); 100 string sourcePath = this.lvResource.SelectedItems[0].SubItems[3].Text;//根路径 101 string desPath = null;//指定路径 102 if (dresult == DialogResult.OK) 103 { 104 desPath = fbd.SelectedPath; 105 desPath += "/" + lvResource.SelectedItems[0].SubItems[0].Text; 106 File.Copy(sourcePath, desPath); 107 MessageBox.Show("复制成功"); 108 } 109 } 110 else 111 { 112 MessageBox.Show("请选择要操作的文件!!"); 113 } 114 } 115 /// <summary> 116 /// 删除 117 /// </summary> 118 /// <param name="sender"></param> 119 /// <param name="e"></param> 120 private void tsmiDel_Click(object sender, EventArgs e) 121 { 122 if (lvResource.SelectedItems.Count > 0) 123 { 124 string DName = lvResource.SelectedItems[0].Text; 125 string path = lvResource.SelectedItems[0].SubItems[3].Text; 126 int index = DName.IndexOf("."); 127 string nName = DName.Substring(0, index); 128 DialogResult result = MessageBox.Show("是否删除" + nName, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 129 if (result == DialogResult.Yes) 130 { 131 File.Delete(path); 132 MessageBox.Show("删除成功"); 133 BindFile(); 134 } 135 } 136 else 137 { 138 MessageBox.Show("请选择要操作的文件!!"); 139 } 140 141 } 142 } 143 }
以上是关于第九章,小型资源管理器的主要内容,如果未能解决你的问题,请参考以下文章