C#实现资源管理器

来源:互联网 发布:土木人的软件 编辑:程序博客网 时间:2024/06/05 18:43




using System;

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

namespace SourceManager
{
    public partial class Form1 : Form
    {
        string[] paths = new string[100];
        int currentPath = 0, maxPath = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TreeNode rootnode = this.treeView1.Nodes[0];
            rootnode.ImageIndex = 0;
            rootnode.Expand();
            TreeNode ndMyComputer = null;
            TreeNode ndDesktop = null;
            foreach (TreeNode nd in rootnode.Nodes)
            {
                if (nd.Name == "ndMyComputer")
                {
                    ndMyComputer = nd;
                }
                if (nd.Name == "ndDesktop")
                {
                    ndDesktop = nd;
                }
                nd.ImageIndex = 0;
            }
            //添加 我的电脑 的子节点
            DriveInfo[] drivers = DriveInfo.GetDrives();
            foreach (DriveInfo driver in drivers)
            {
                TreeNode node = new TreeNode(driver.Name.Substring(0, driver.Name.Length-1));
                node.ImageIndex = 0;
                node.Tag = driver.RootDirectory.FullName.Substring(0, driver.Name.Length - 1);
                ndMyComputer.Nodes.Add(node);
            }
            //添加 桌面 的子节点
            string DesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            ndDesktop.Tag = DesktopPath;
            DirectoryInfo MyDocument = new DirectoryInfo(DesktopPath);
            DirectoryInfo[] folders = MyDocument.GetDirectories();
            FileInfo[] files = MyDocument.GetFiles();
            foreach (DirectoryInfo folder in folders)
            {
                TreeNode node = new TreeNode(folder.Name);
                node.Tag = folder.FullName;
                node.ImageIndex = 0;
                node.Name = folder.LastWriteTime.ToString();
                ndDesktop.Nodes.Add(node);
            }
            foreach (FileInfo file in files)
            {
                TreeNode node = new TreeNode(file.Name);
                switch (file.Extension)
                {
                    case ".rar":
                    case ".zip":
                        node.ImageIndex = 2;
                        break;
                    case ".wps":
                    case ".doc":
                        node.ImageIndex = 3;
                        break;
                    case ".xls":
                        node.ImageIndex = 4;
                        break;
                    case ".xv":
                    case ".avi":
                    case ".flv":
                    case ".rmvb":
                        node.ImageIndex = 5;
                        break;
                    case ".jpg":
                    case ".png":
                    case ".ico":
                        node.ImageIndex = 6;
                        break;
                    default:
                        node.ImageIndex = 1;
                        break;
                }
                node.Tag = file.FullName;
                node.Name = file.LastWriteTime.ToString();
                ndDesktop.Nodes.Add(node);
            }
        }

        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {

            if (e.Node.Parent == null)
            {
                return;
            }
            foreach (TreeNode node in e.Node.Nodes)
            {
                if (node.Tag == null)
                {
                    continue;
                }
                DirectoryInfo folder = new DirectoryInfo(node.Tag.ToString());
                DirectoryInfo[] subFolders = null;
                FileInfo[] subFiles = null;
                try
                {
                    subFolders = folder.GetDirectories();
                    subFiles = folder.GetFiles();
                }
                catch (Exception)
                {
                }
                if (subFolders != null)
                {
                    foreach (DirectoryInfo subFolder in subFolders)
                    {
                        TreeNode subNode = new TreeNode(subFolder.Name);
                        subNode.Name = subFolder.LastWriteTime.ToString();
                        subNode.Tag = subFolder.FullName;
                        subNode.ImageIndex = 0;
                        bool isExit = false;
                        foreach (TreeNode exitNode in node.Nodes)
                        {
                            if (exitNode.Text == subNode.Text)
                            {
                                isExit = true;
                                break;
                            }
                        }
                        if (!isExit)
                            node.Nodes.Add(subNode);
                    }
                }
                if (subFiles != null)
                {
                    foreach (FileInfo file in subFiles)
                    {
                        TreeNode subNode = new TreeNode(file.Name);
                        subNode.Tag = file.FullName;
                        subNode.Name = file.LastWriteTime.ToString();
                        switch (file.Extension)
                        {
                            case ".txt":
                                subNode.ImageIndex = 1;
                                break;
                            case ".rar":
                            case ".zip":
                                subNode.ImageIndex = 2;
                                break;
                            case ".wps":
                            case ".doc":
                                subNode.ImageIndex = 3;
                                break;
                            case ".xls":
                                subNode.ImageIndex = 4;
                                break;
                            case ".xv":
                            case ".avi":
                            case ".flv":
                            case ".rmvb":
                            case ".3gp":
                            case ".wmv":
                            case ".mp4":
                                subNode.ImageIndex = 5;
                                break;
                            case ".jpg":
                            case ".png":
                            case ".ico":
                                subNode.ImageIndex = 6;
                                break;
                            default:
                                subNode.ImageIndex = 1;
                                break;
                        }
                        bool isExit = false;
                        foreach (TreeNode exitNode in node.Nodes)
                        {
                            if (exitNode.Text == subNode.Text)
                            {
                                isExit = true;
                                break;
                            }
                        }
                        if (!isExit)
                            node.Nodes.Add(subNode);
                    }
                }
            }
            //MessageBox.Show("expand", "提示信息");
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Text == "资源管理器")
            {
                return;
            }
            bool isFolder = false;
            if (e.Node.ImageIndex != 0) {
                toolStripComboBox1.Text = e.Node.Parent.Tag + "";
            }
            else{
                toolStripComboBox1.Text = e.Node.Tag+"";//路径
            }
            e.Node.SelectedImageIndex = e.Node.ImageIndex;
            if (toolStripComboBox1.Text != "")
            {
                currentPath = maxPath;
                paths[maxPath] = toolStripComboBox1.Text;
                maxPath++;
            }
            this.listView1.Items.Clear();
            this.listView1.BeginUpdate(); //数据更新,UI暂时挂起,直到EndUpdate绘制控件,可以有效避免闪烁并大大提高加载速度
            foreach (TreeNode son in e.Node.Nodes) {
                ListViewItem lvi = new ListViewItem();
                lvi.StateImageIndex = son.ImageIndex;
                lvi.Text = son.Text;
                lvi.SubItems.Add(son.Name);
                if (lvi.StateImageIndex == 0)
                {
                    lvi.SubItems.Add("");
                }
                else {
                    try{
                        FileInfo file = new FileInfo(son.Tag.ToString());
                        lvi.SubItems.Add((file.Length/1000).ToString() + "KB");
                    }
                    catch (Exception) { }
                }
                this.listView1.Items.Add(lvi);
                isFolder = true;
            }
            if (e.Node.IsExpanded == false && isFolder == false)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.StateImageIndex = e.Node.ImageIndex;
                lvi.Text = e.Node.Text;
                lvi.SubItems.Add(e.Node.Name);
                try
                {
                    //MessageBox.Show(toolStripComboBox1.Text, "");
                    FileInfo file = new FileInfo(toolStripComboBox1.Text + "\\" + lvi.Text);
                    lvi.SubItems.Add((file.Length/1000).ToString()+"KB");
                }
                catch { }
                this.listView1.Items.Add(lvi);
            }
            this.listView1.EndUpdate();  //结束数据处理,UI界面一次性绘制。
        }

        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            foreach (ListViewItem lvi in listView1.SelectedItems)
            {
               if (lvi.StateImageIndex == 0)
                {
                    DirectoryInfo dir = null;
                    DirectoryInfo[] subdirs = null;
                    FileInfo[] subfiles = null;
                    try
                    {
                        if (toolStripComboBox1.Text == "")
                            dir = new DirectoryInfo(@lvi.Text + "\\");
                        else
                            dir = new DirectoryInfo(@toolStripComboBox1.Text + "\\" + lvi.Text);
                        subdirs = dir.GetDirectories();
                        subfiles = dir.GetFiles();
                    }
                    catch (Exception)
                    {
                        return;
                    }
                    this.listView1.Items.Clear();
                    this.listView1.BeginUpdate();
                    foreach (DirectoryInfo son in subdirs)
                    {
                        ListViewItem sublvi = new ListViewItem();
                        sublvi.StateImageIndex = 0;
                        sublvi.Text = son.Name;
                        sublvi.SubItems.Add(son.LastWriteTime.ToString());
                        sublvi.SubItems.Add("");
                        this.listView1.Items.Add(sublvi);;
                    }
                    foreach (FileInfo son in subfiles)
                    {
                        ListViewItem sublvi = new ListViewItem();
                        sublvi.Text = son.Name;
                        sublvi.SubItems.Add(son.LastWriteTime.ToString());
                        switch (son.Extension)
                        {
                            case ".txt":
                                sublvi.StateImageIndex = 1;
                                break;
                            case ".rar":
                            case ".zip":
                                sublvi.StateImageIndex = 2;
                                break;
                            case ".wps":
                            case ".doc":
                                sublvi.StateImageIndex = 3;
                                break;
                            case ".xls":
                                sublvi.StateImageIndex = 4;
                                break;
                            case ".xv":
                            case ".avi":
                            case ".flv":
                            case ".rmvb":
                            case ".3gp":
                            case ".wmv":
                            case ".mp4":
                                sublvi.StateImageIndex = 5;
                                break;
                            case ".jpg":
                            case ".png":
                            case ".ico":
                                sublvi.StateImageIndex = 6;
                                break;
                            default:
                                sublvi.StateImageIndex = 1;
                                break;
                        }
                        sublvi.SubItems.Add((son.Length/ 1000).ToString() + "KB");
                        this.listView1.Items.Add(sublvi);
                    }
                    this.listView1.EndUpdate();
                    //路径
                    if (toolStripComboBox1.Text == "")
                        toolStripComboBox1.Text = lvi.Text;
                    else
                        toolStripComboBox1.Text = toolStripComboBox1.Text + "\\" + lvi.Text;
                    if (toolStripComboBox1.Text != "")
                    {
                        currentPath = maxPath;
                        paths[maxPath] = toolStripComboBox1.Text;
                        maxPath++;
                    }
                }
                if (lvi.StateImageIndex != 0)
                {
                    try
                    {
                        System.Diagnostics.Process.Start(toolStripComboBox1.Text + "\\" + lvi.Text);
                    }
                    catch(Exception){
                    }
                }
            }

        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (currentPath > 0)
            {
                currentPath--;
                toolStripComboBox1.Text = paths[currentPath];
                updateListView(toolStripComboBox1.Text);
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (currentPath < maxPath && paths[1 + currentPath] != "" && paths[1 + currentPath] != null)
            {
                currentPath++;
                toolStripComboBox1.Text = paths[currentPath];
                updateListView(toolStripComboBox1.Text);
            }
        }

        private void updateListView(string path)
        {
            DirectoryInfo dir = null;
            DirectoryInfo[] subdirs = null;
            FileInfo[] subfiles = null;
            try
            {
                if (path != "")
                {
                    dir = new DirectoryInfo(@path);
                }
                subdirs = dir.GetDirectories();
                subfiles = dir.GetFiles();
            }
            catch (Exception)
            {
                return;
            }
            this.listView1.Items.Clear();
            this.listView1.BeginUpdate();
            foreach (DirectoryInfo son in subdirs)
            {
                ListViewItem sublvi = new ListViewItem();
                sublvi.StateImageIndex = 0;
                sublvi.Text = son.Name;
                sublvi.SubItems.Add(son.LastWriteTime.ToString());
                sublvi.SubItems.Add("");//大小
                this.listView1.Items.Add(sublvi); ;
            }
            foreach (FileInfo son in subfiles)
            {
                ListViewItem sublvi = new ListViewItem();
                sublvi.Text = son.Name;
                sublvi.SubItems.Add(son.LastWriteTime.ToString());
                switch (son.Extension)
                {
                    case ".txt":
                        sublvi.StateImageIndex = 1;
                        break;
                    case ".rar":
                    case ".zip":
                        sublvi.StateImageIndex = 2;
                        break;
                    case ".wps":
                    case ".doc":
                        sublvi.StateImageIndex = 3;
                        break;
                    case ".xls":
                        sublvi.StateImageIndex = 4;
                        break;
                    case ".xv":
                    case ".avi":
                    case ".flv":
                    case ".rmvb":
                    case ".3gp":
                    case ".wmv":
                    case ".mp4":
                        sublvi.StateImageIndex = 5;
                        break;
                    case ".jpg":
                    case ".png":
                    case ".ico":
                        sublvi.StateImageIndex = 6;
                        break;
                    default:
                        sublvi.StateImageIndex = 1;
                        break;
                }
                sublvi.SubItems.Add((son.Length/1000).ToString()+"KB");
                this.listView1.Items.Add(sublvi);
            }
            this.listView1.EndUpdate();        
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            updateListView(toolStripComboBox1.Text);
        }

        private void 新建文件夹ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string dirName = "\\新建文件夹";
            if (toolStripComboBox1.Text != "")
            {
                Directory.CreateDirectory(toolStripComboBox1.Text + dirName);
                updateListView(toolStripComboBox1.Text);
            }
        }

        private void 新建文本ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string fileName = "\\新建文本文件.txt";
            if (toolStripComboBox1.Text != "")
            {
                File.Create(toolStripComboBox1.Text + fileName).Close();
                updateListView(toolStripComboBox1.Text);
            }
        }

        private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("檀国林的博客 http://blog.csdn.net/wotgl", "帮助");
        }
    }
}
原创粉丝点击