-
背景
- TreeView
- ListView
- 文件操作
-
用到的语句
- TreeView
- 1TreeNode tn = new TreeNode(item.Name);//括号里是文本内容
1. 2 treeView1.Nodes.Add(tn);
2. ListView
2.1
public class MyFile
{
public float FileLength { get; set; }//文件长度(KB)
public string FileName { get; set; }//文件名
public string FilePath { get; set; }//文件路径
public string FileType { get; set; }//文件类型}
2.2 添加操作
ListViewItem lv = new ListViewItem(em.FileName);
lv.SubItems.Add(em.FileLength.ToString());
//subItems为每一类添加内容
lv.SubItems.Add(em.FileType);
lv.SubItems.Add(em.FilePath);
listView1.Items.Add(lv);
2.3 删除操作
File.Delete(delepath);
listView1.Items.Remove(listView1 .FocusedItem);
MessageBox.Show("删除成功");
2.4 获取路径
string sourcepath = listView1.SelectedItems[0].SubItems[3].Text;
- 文件操作
- DriveInfo[] di = DriveInfo.GetDrives(); //获取磁盘内容
- DirectoryInfo directoryInfo = new DirectoryInfo("D:\\test");//文件夹操作,创建文件夹实例
- DirectoryInfo[] dirs = dir.GetDirectories();//返回当前文件夹的子文件夹
- FileInfo[] fi = dir.GetFiles();//FileInfo文件类,进行文件操作,此句即为从文件夹中获取并返回文件列表
- File.Delete(delepath);//删除文件
- 参数一:要复制的文件路径;参数二:复制出来的新的文件路径及文件名字
File.Copy(sourcepath, despath);
7. Process.Start(openpath);//打开文件
-
步骤
- step1 建立窗口
step2.获取磁盘驱动器信息
private void Form1_Load(object sender, EventArgs e){DriveInfo[] di = DriveInfo.GetDrives();foreach (DriveInfo item in di){TreeNode tn = new TreeNode(item.Name);tn.Tag = item.Name;treeView1.Nodes.Add(tn);}}
step3.建立MyFile类,用于存储列表中的文件信息
public class MyFile{public float FileLength { get; set; }//文件长度(KB)public string FileName { get; set; }//文件名public string FilePath { get; set; }//文件路径public string FileType { get; set; }//文件类型}
step4.显示子节点和列表中的项
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e){TreeNode node = this.treeView1.SelectedNode;this.BindInfo(node);}private void BindInfo(TreeNode node){//clearlistView1.Items.Clear();//bind child contentsDirectoryInfo dir = new DirectoryInfo(node.Tag.ToString());//获取当前选中文件夹的实例DirectoryInfo[] dirs = dir.GetDirectories();//获取当前文件夹的子文件夹foreach (DirectoryInfo di in dirs)//循环为每个子文件夹创立结点{TreeNode temp = new TreeNode();temp.Text = di.Name;temp.Tag = di.FullName;node.Nodes.Add(temp);//添加到树中}FileInfo[] fi = dir.GetFiles();//返回文件夹中的所有子文件List<MyFile> files = new List<MyFile>();//files'messageforeach(FileInfo myFile in fi)//循环存储每个子文件的文件信息{MyFile mf = new MyFile();mf.FileName = myFile.Name;mf.FileLength = myFile.Length;mf.FileType = myFile.Extension;mf.FilePath = myFile.FullName;files.Add(mf);//存到List容器中}foreach(MyFile em in files)//为每个List中的文件创造ListVIew中的项{ListViewItem lv = new ListViewItem(em.FileName);lv.SubItems.Add(em.FileLength.ToString());//添加列中的具体内容lv.SubItems.Add(em.FileType);lv.SubItems.Add(em.FilePath);listView1.Items.Add(lv);}}
step5.删除操作
private void 删除ToolStripMenuItem_Click(object sender, EventArgs e){string delepath = listView1.SelectedItems[0].SubItems[3].Text;try{File.Delete(delepath);listView1.Items.Remove(listView1 .FocusedItem);MessageBox.Show("删除成功");}catch (Exception ex){MessageBox.Show(ex.Message);}}
step6.复制操作
private void 复制ToolStripMenuItem_Click(object sender, EventArgs e){if(listView1 .SelectedItems .Count ==0){MessageBox.Show("请选中一个文件");return;}FolderBrowserDialog fbd = new FolderBrowserDialog();DialogResult result = fbd.ShowDialog();string sourcepath = listView1.SelectedItems[0].SubItems[3].Text;string despath = null;if(result==DialogResult.OK){despath = fbd.SelectedPath;despath += "\\" + listView1.SelectedItems[0].SubItems[0].Text;try{File.Copy(sourcepath, despath);MessageBox.Show("复制成功");}catch (Exception ex){MessageBox.Show(ex.Message);}}}
step6.复制操作
private void button1_Click(object sender, EventArgs e){string openpath = listView1.SelectedItems[0].SubItems[3].Text;try{ //打开文件Process.Start(openpath);}catch (Exception ex){MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);}}
运行实况
主窗口全部代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;namespace 资源管理laplace
{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void Form1_Load(object sender, EventArgs e){DriveInfo[] di = DriveInfo.GetDrives();foreach (DriveInfo item in di){TreeNode tn = new TreeNode(item.Name);tn.Tag = item.Name;treeView1.Nodes.Add(tn);}}private void treeView1_AfterSelect(object sender, TreeViewEventArgs e){TreeNode node = this.treeView1.SelectedNode;this.BindInfo(node);}private void BindInfo(TreeNode node){//clearlistView1.Items.Clear();//bind child contentsDirectoryInfo dir = new DirectoryInfo(node.Tag.ToString());//获取当前选中文件夹的实例DirectoryInfo[] dirs = dir.GetDirectories();//获取当前文件夹的子文件夹foreach (DirectoryInfo di in dirs)//循环为每个子文件夹创立结点{TreeNode temp = new TreeNode();temp.Text = di.Name;temp.Tag = di.FullName;node.Nodes.Add(temp);//添加到树中}FileInfo[] fi = dir.GetFiles();//返回文件夹中的所有子文件List<MyFile> files = new List<MyFile>();//files'messageforeach(FileInfo myFile in fi)//循环存储每个子文件的文件信息{MyFile mf = new MyFile();mf.FileName = myFile.Name;mf.FileLength = myFile.Length;mf.FileType = myFile.Extension;mf.FilePath = myFile.FullName;files.Add(mf);//存到List容器中}foreach(MyFile em in files)//为每个List中的文件创造ListVIew中的项{ListViewItem lv = new ListViewItem(em.FileName);lv.SubItems.Add(em.FileLength.ToString());//添加列中的具体内容lv.SubItems.Add(em.FileType);lv.SubItems.Add(em.FilePath);listView1.Items.Add(lv);}}private void 删除ToolStripMenuItem_Click(object sender, EventArgs e){string delepath = listView1.SelectedItems[0].SubItems[3].Text;try{File.Delete(delepath);listView1.Items.Remove(listView1 .FocusedItem);MessageBox.Show("删除成功");}catch (Exception ex){MessageBox.Show(ex.Message);}}private void 复制ToolStripMenuItem_Click(object sender, EventArgs e){if(listView1 .SelectedItems .Count ==0){MessageBox.Show("请选中一个文件");return;}FolderBrowserDialog fbd = new FolderBrowserDialog();DialogResult result = fbd.ShowDialog();string sourcepath = listView1.SelectedItems[0].SubItems[3].Text;string despath = null;if(result==DialogResult.OK){despath = fbd.SelectedPath;despath += "\\" + listView1.SelectedItems[0].SubItems[0].Text;try{File.Copy(sourcepath, despath);MessageBox.Show("复制成功");}catch (Exception ex){MessageBox.Show(ex.Message);}}}private void listView1_SelectedIndexChanged(object sender, EventArgs e){}private void button1_Click(object sender, EventArgs e){string openpath = listView1.SelectedItems[0].SubItems[3].Text;try{ //打开文件Process.Start(openpath);}catch (Exception ex){MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);}}}public class MyFile{public float FileLength { get; set; }//文件长度(KB)public string FileName { get; set; }//文件名public string FilePath { get; set; }//文件路径public string FileType { get; set; }//文件类型}
}