zoukankan      html  css  js  c++  java
  • 第九章课后作业

    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

  • 相关阅读:
    算法Sedgewick第四版-第1章基础-014一用stack把前置表达式转为后置表达式并计算值
    算法Sedgewick第四版-第1章基础-013一用stack实现自动补全表达式括号
    算法Sedgewick第四版-第1章基础-012一用stack实现输出一个数的二进制形式
    算法Sedgewick第四版-第1章基础-011一用链表实现bag、queue、stack
    算法Sedgewick第四版-第1章基础-010一检查括号是否成对出现
    算法Sedgewick第四版-第1章基础-009一链表与数组的比较及其他数据结构
    算法Sedgewick第四版-第1章基础-008一用数组实现栈(泛型、可变大小)
    算法Sedgewick第四版-第1章基础-007一用两个栈实现简单的编译器
    webApi2 上传大文件代码
    IE8 AJAX 不能正常工作 解决办法
  • 原文地址:https://www.cnblogs.com/zhangxiaoyu123/p/6724684.html
Copyright © 2011-2022 走看看