C#資源管理器


窗體搭建:ContextMenuStrip右鍵菜單,Treeview樹形菜單,Listview控件

新建"我的文件"類:

public class MyFile
    {
        public float FileLength { get; set; }//文件長度(KB)
        public string FileName { get; set; }//文件名
        public string FilePath { get; set; }//文件路徑
        public string FileType { get; set; }//文件類型
    }

主窗體全部代碼:

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;

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

        private void tvExplore_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = this.tvExplore.SelectedNode;
            this.BindInfo(node);
        }
        //將目錄綁定到Treeview中
        private void BindInfo(TreeNode node)
        {
            //清空
            lvInfo.Items.Clear();
            //綁定子目錄
            DirectoryInfo 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);
            }
            //獲取目錄下文件列表,dir是目錄對象
            FileInfo [] fi = dir.GetFiles();
            //定義反省集合存儲文件信息
            List<MyFile> files = new List<MyFile>();
            //遍歷文件列表
            foreach (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);
            }
            //綁定到Listview中
            foreach (MyFile em in files)
            {
                ListViewItem lv = new ListViewItem(em.FileName);
                lv.SubItems.Add(em.FileLength.ToString());
                lv.SubItems.Add(em.FileType);
                lv.SubItems.Add(em.FilePath);
                lvInfo.Items.Add(lv);
            }
        }
        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;
                tvExplore.Nodes.Add(tn);
            }
        }
        private void 復制文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //判斷是否選中,沒選中return
            if (lvInfo.SelectedItems.Count == 0)
            {
                MessageBox.Show("請選中一個文件");
                return;
            }
            //提示用戶選擇目標文件夾
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult result = fbd.ShowDialog();
            //源文件路徑
            string sourcepath = lvInfo.SelectedItems[0].SubItems[3].Text;
            //目標文件路徑
            string despath = null;
            //如果正確選擇目標位置,執行復制操作
            if (result == DialogResult.OK)
            {
                despath = fbd.SelectedPath;
                //lvlist表示顯示文件信息的ListView對象
                despath += "\\" + lvInfo.SelectedItems[0].SubItems[0].Text;
                //復制文件
                try
                {
                    File.Copy(sourcepath, despath);
                    MessageBox.Show("復制成功!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                
            }
        }
        private void 刪除文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string delectpath = lvInfo.SelectedItems[0].SubItems[3].Text;
            try
            {
                File.Delete(delectpath);
                MessageBox.Show("刪除成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM