第九章课后作业

本文介绍了一个使用C#实现的文件系统操作示例,包括加载指定路径下的目录和文件,并展示如何进行复制与删除等基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 lesson9_34
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            loadRootNode();
        }
        //添加磁盘
        private void loadRootNode()
        {
            TreeNode node = new TreeNode();
            node.Text = "D:\\";
            node.Tag = "D:\\";
            this.tvFile.Nodes.Add(node);
        }
        private void BindInfo(TreeNode node)
        {
            //绑定子目录
            DirectoryInfo DI = new DirectoryInfo(node.Tag.ToString());
            DirectoryInfo[] dirs = DI.GetDirectories();
            foreach (DirectoryInfo di in dirs)
            {
                TreeNode temp = new TreeNode();
                temp.Text = di.Name;
                temp.Tag = di.FullName;
                node.Nodes.Add(temp);
            }
            //绑定目录的文件
            FileInfo[] fileInfo = DI.GetFiles();
            List<MyFile> files = new List<MyFile>();
            foreach (FileInfo myfile in fileInfo)
            {
                MyFile file = new MyFile();
                file.FileName = myfile.Name;
                file.FileLength = myfile.Length;
                file.FilePath = myfile.FullName;
                file.FileType = myfile.Extension;
                files.Add(file);
            }
            //绑定Listview
            ListViewItem item = null;
            this.lvFile.Items.Clear();
            foreach (MyFile file in files)
            {
                item = new ListViewItem();
                item.Text = file.FileName;
                item.SubItems.Add(file.FileLength.ToString());
                item.SubItems.Add(file.FileType);
                item.SubItems.Add(file.FilePath);
                this.lvFile.Items.Add(item);
            }
        }

        private void tvFile_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode node = this.tvFile.SelectedNode;
            this.BindInfo(node);
        }
        //Copy复制
        private void tsmiCopy_Click(object sender, EventArgs e)
        {
            if (this.lvFile.SelectedItems.Count == 0)
            {
                return;
            }
            FolderBrowserDialog fb = new FolderBrowserDialog();
            DialogResult result = fb.ShowDialog();
            //提示用户选择文件
            string sourcePath = lvFile.SelectedItems[0].SubItems[3].Text;
            //源文件路径
            string desPath = null;
            //正确选择,执行复制操作
            if (result == DialogResult.OK)
            {   
                try
                {
                    desPath = fb.SelectedPath;
                    desPath += "/" + lvFile.SelectedItems[0].SubItems[0].Text;
                    //复制文件
                    File.Copy(sourcePath, desPath);
                    MessageBox.Show("复制成功!");
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }
            }
        }
        //删除文件
        private void tsmiDelete_Click(object sender, EventArgs e)
        {
            if (this.lvFile.SelectedItems.Count == 0)
            {
                return;
            }
            //删除的文件
            string sourcePath = lvFile.SelectedItems[0].SubItems[3].Text;
            DialogResult result = MessageBox.Show("确定要删除?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                File.Delete(sourcePath);
                MessageBox.Show("删除成功!");
            }
            //刷新一下
            this.lvFile.SelectedItems[0].Remove();
        }

    }
}
主窗体
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lesson9_34
{
    public class MyFile
    {
         //文件路径
        public string FilePath { get; set; }
        //文件大小
        public float FileLength { get; set; }
        //文件名
        public string FileName { get; set; }
        //文件类型
        public string FileType { get; set; }
    }
}
MyFile

转载于:https://www.cnblogs.com/zhangxiaoyu123/p/6724684.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值