C# 小型資源管理器


 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 小型資源管理器
{
    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 小型資源管理器
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        TreeNode tn;
        private void Form1_Load(object sender, EventArgs e)
        {
            //檢索計算機上的所有邏輯驅動器的驅動器名稱。
            DriveInfo[] di = DriveInfo.GetDrives();
            foreach (DriveInfo item in di)
            {
                tn = new TreeNode(item.Name);
                tn.Tag = item.Name;
                tv01.Nodes.Add(tn);
            }
        }
        
        //單擊綁定文件和文件夾信息
        private void tv01_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode tn= tv01.SelectedNode;
            BingInfo(tn);

        }

       
        private void BingInfo(TreeNode tn)
        {
            //清空
            lvlist.Items.Clear();
            //綁定子目錄
            DirectoryInfo dic = new DirectoryInfo(tn.Tag.ToString());
            DirectoryInfo[] info = dic.GetDirectories();
            foreach (DirectoryInfo item in info)
            {
                TreeNode temp = new TreeNode();
                temp.Text = item.Name;
                temp.Tag = item.FullName;
                tn.Nodes.Add(temp);
            }

            //獲取目錄下文件列表
            FileInfo[] fileinfo = dic.GetFiles();
            //定義泛型集合存儲文件信息
            List<MyFile> files = new List<MyFile>();
            //遍歷文件列表
            foreach (FileInfo  it in fileinfo )
            {

                MyFile file = new MyFile();
                file.FileName = it.Name;
                file.FileLength = it.Length;
                file.FileType = it.Extension;
                file.FilePath = it.FullName;
                files.Add(file);
            }
            //綁定到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 );
                lvlist.Items.Add(lv);
            }

           
        }

        //復制
        private void 復制ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //判斷是否選中
            if (lvlist .SelectedItems .Count ==0)
            {
                return;
            }
            //提示用戶選擇目標文件夾
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            DialogResult result = fbd.ShowDialog();
            //源文件路徑
            string sourcepath = lvlist.SelectedItems[0].SubItems[3].Text;
            //目標文件路徑
            string despath = null;
            //如果正確選擇目標位置,執行復制操作
            if (result==DialogResult .OK )
            {
                despath = fbd.SelectedPath;
                //lvlist表示顯示文件信息的ListView對象
                despath += "\\" + lvlist.SelectedItems[0].SubItems[0].Text;
                //復制文件
                File.Copy(sourcepath ,despath );
                MessageBox.Show("復制成功!");
            }

           
        }

        //刪除
        private void 刪除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            string delectpath= lvlist.SelectedItems[0].SubItems [3].Text ;
            File.Delete(delectpath);
            MessageBox.Show("刪除成功!");
        }

        

    }
}

 


免責聲明!

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



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