c#程序开发案例课堂第9章习题解答-练习1-实现文件和文件夹的复制和删除功能

练习1

  • 界面
    注意不要忽略3个dialoge控件

    Design.PNG

  • 代码

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


        /*
        private void listBoxCopy_Click(object sender, EventArgs e)
        {
            if(listBoxCopy.SelectedIndex==0)
            {
                //选择的是复制文件
                if(OpenFileDialog.ShowDialog()==DialogResult.OK)
                {
                    textBoxSource.Text = OpenFileDialog.FileName;
                }
            }
        }
        */

        //判断是否选择了某种操作
        public bool IsFun()
        {
            bool MyBool = false;
            if (listBoxCopy.SelectedIndex != -1)
            {
                MyBool = true;
            }
            return MyBool;
        }

        //复制文件夹下的子文件夹及其文件
        public static void CopyDirectory(string srcPath, string destPath)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(srcPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //获取目录下(不包含子目录)的文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)     //判断是否文件夹
                    {
                        if (!Directory.Exists(destPath + "\\" + i.Name))
                        {
                            Directory.CreateDirectory(destPath + "\\" + i.Name);   //目标目录下不存在此文件夹即创建子文件夹
                        }
                        CopyDirectory(i.FullName, destPath + "\\" + i.Name);    //递归调用复制子文件夹
                    }
                    else
                    {
                        File.Copy(i.FullName, destPath + "\\" + i.Name, true);      //不是文件夹即复制文件,true表示可以覆盖同名文件
                    }
                }
            }
            catch (Exception e)
            {
                throw;
            }
        }

        private void textBoxDestination_MouseClick(object sender, MouseEventArgs e)
        {
            
        }//没有用

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }//没有用

        //确认键
        private void button1_Click(object sender, EventArgs e)
        {
            if(textBoxSource.Text=="")
            {
                MessageBox.Show("源路径为空!");
            }
            else if((listBoxCopy.SelectedIndex == 0 || listBoxCopy.SelectedIndex == 2)
                &&(textBoxDestination.Text==""))
            {
                //只在复制时报错,删除没有必要检查为空
                MessageBox.Show("目标路径为空!");
            }
            
            else
            {
                if (listBoxCopy.SelectedIndex == 0)
                {
                    File.Copy(textBoxSource.Text, textBoxDestination.Text);
                    btnDestination.Enabled = true;
                }
                else if(listBoxCopy.SelectedIndex == 1)
                {
                    File.Delete(textBoxSource.Text);
                    btnDestination.Enabled = true;
                }
                else if(listBoxCopy.SelectedIndex == 2)
                {
                    CopyDirectory(textBoxSource.Text,textBoxDestination.Text);
                    btnDestination.Enabled = true;
                }
                else if (listBoxCopy.SelectedIndex == 3)
                {
                    Directory.Delete(textBoxSource.Text,true);
                    btnDestination.Enabled = true;
                }
            }

            textBoxSource.Clear();
            textBoxDestination.Clear();

        }

       //选择目标路径
        private void btnDestination_Click(object sender, EventArgs e)
        {
            if (!IsFun())
            {
                MessageBox.Show("未选择操作!");
            }
            else 
            {
                if (listBoxCopy.SelectedIndex == 1 || listBoxCopy.SelectedIndex == 3)
                {
                    btnDestination.Enabled = false;
                    MessageBox.Show("这是删除操作,不需要目的路径!");
                }
                //打开保存目录
                else if (listBoxCopy.SelectedIndex == 0)
                {
                    if(saveFileDialog1.ShowDialog()==DialogResult.OK)
                    {
                        textBoxDestination.Text = saveFileDialog1.FileName;
                    }
                }
                else if (listBoxCopy.SelectedIndex == 2)
                {
                    if (folderBrower.ShowDialog() == DialogResult.OK)
                    {
                        textBoxDestination.Text = folderBrower.SelectedPath;
                        CopyDirectory(textBoxSource.Text,textBoxDestination.Text);
                    }
                }
            }
            
            
        }
        //选择源路径
        private void btnSouce_Click(object sender, EventArgs e)
        {
            if (!IsFun())
            {
                MessageBox.Show("未选择操作!");
            }
            else if (listBoxCopy.SelectedIndex == 0 || listBoxCopy.SelectedIndex == 1)
            {
                //打开文件
                if (OpenFileDialog.ShowDialog() == DialogResult.OK)
                {
                    textBoxSource.Text = OpenFileDialog.FileName;
                }
                
            }
            else
            {
                //打开目录
                if (folderBrower.ShowDialog() == DialogResult.OK)
                {
                    textBoxSource.Text = folderBrower.SelectedPath;
                }
            }



        }
    }
}

你可能感兴趣的:(c#程序开发案例课堂第9章习题解答-练习1-实现文件和文件夹的复制和删除功能)