小型资源管理器案列(对文件的常用操作)

 1.在选中子目录的时候刷新数据,防止一次性刷新太多数据,造成“盲等”,延迟程序启动时间

  //在选中子目录的时候刷新数据,防止一次性刷新太多数据,造成“盲等”,延迟程序启动时间
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = tvList.SelectedNode;
            //得到当前选项的  
            DirectoryInfo di = new DirectoryInfo(tvList.SelectedNode.Tag.ToString());//创建目录对象 
            DirectoryInfo[] dirs = di.GetDirectories();//得到子节点
            node.Nodes.Clear();
            //循环 绑定数据
            foreach (DirectoryInfo d in dirs)
            {
                TreeNode node2 = new TreeNode();
                node2.Text = d.Name;
                node2.Tag = d.FullName;
                node.Nodes.Add(node2);
            }
            //得到当前目录下的子文件
            FileInfo[] files = di.GetFiles();
            lvList.Items.Clear();//清空
            foreach (FileInfo fi in files)
            {
                //放到ListView
                ListViewItem lvi = new ListViewItem();
                lvi.Text = fi.Name;//根节点
                lvi.SubItems.Add(fi.Extension);//添加子节点 
                lvi.SubItems.Add((fi.Length / 1024.0).ToString());
                lvi.SubItems.Add(fi.FullName);
                lvList.Items.Add(lvi);
            }

        }

2.双击显示子目录文件

 //选中一行,双击打开文件
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            string path = this.lvList.SelectedItems[0].SubItems[3].Text;
            System.Diagnostics.Process.Start(path);
        }

 

3.其他代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SmallRescourceManagere
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //添加根节点
            disPlayView();       
        }
        public void disPlayView()
        {
            //添加根节点
            TreeNode nodeRoot = new TreeNode("E:\\");
            //nodeRoot.Text = "D:\\";
            nodeRoot.Tag = "E:\\"; //完整的路径
            tvList.Nodes.Add(nodeRoot);
        }
        //选中一行,双击打开文件
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            string path = this.lvList.SelectedItems[0].SubItems[3].Text;
            System.Diagnostics.Process.Start(path);
        }
        //在选中子目录的时候刷新数据,防止一次性刷新太多数据,造成“盲等”,延迟程序启动时间
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = tvList.SelectedNode;
            //得到当前选项的  
            DirectoryInfo di = new DirectoryInfo(tvList.SelectedNode.Tag.ToString());//创建目录对象 
            DirectoryInfo[] dirs = di.GetDirectories();//得到子节点
            node.Nodes.Clear();
            //循环 绑定数据
            foreach (DirectoryInfo d in dirs)
            {
                TreeNode node2 = new TreeNode();
                node2.Text = d.Name;
                node2.Tag = d.FullName;
                node.Nodes.Add(node2);
            }
            //得到当前目录下的子文件
            FileInfo[] files = di.GetFiles();
            lvList.Items.Clear();//清空
            foreach (FileInfo fi in files)
            {
                //放到ListView
                ListViewItem lvi = new ListViewItem();
                lvi.Text = fi.Name;//根节点
                lvi.SubItems.Add(fi.Extension);//添加子节点 
                lvi.SubItems.Add((fi.Length / 1024.0).ToString());
                lvi.SubItems.Add(fi.FullName);
                lvList.Items.Add(lvi);
            }

        }

        private void 复制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //复制文件
            //浏览文件的对话廓
            FolderBrowserDialog fdb = new FolderBrowserDialog();
            DialogResult resulet = fdb.ShowDialog();
            //判断用户是否确定
            if (resulet == DialogResult.OK)
            {
                string path = lvList.SelectedItems[0].SubItems[3].Text;
                string pas = fdb.SelectedPath + "\\" + lvList.SelectedItems[0].Text;
                File.Copy(path, pas, true);
                MessageBox.Show("大爷,文件复制成功");
            }
            else
            {
                MessageBox.Show("请选择复制文件");
            }

        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //删除文件
            if (lvList.SelectedItems.Count == 0)
            {
                MessageBox.Show("请选择一行");
                return;
            }
            //提示用户选择目标文件
            else
            {
                DialogResult result = MessageBox.Show("确定删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (result == DialogResult.OK)
                {
                    string patth = lvList.SelectedItems[0].SubItems[3].Text;
                    File.Delete(patth);//删除文件
                    lvList.SelectedItems[0].Remove();//删除lv的数据
                    MessageBox.Show("大爷删除成功!");
                }
            }
        }
    }
}

 

posted on 2018-04-20 17:58  技术之路永无止境~  阅读(159)  评论(0编辑  收藏  举报

导航