using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();          //浏览文件夹对话框
            textBox1.Text = folderBrowserDialog1.SelectedPath;    //提取源目录路径及名称
            if (textBox1.Text != "")
            {
                DirectoryInfo dir = new DirectoryInfo(textBox1.Text);
                FileInfo[] f = dir.GetFiles();                //提取源目录中所有文件
                for (int i = 0; i < f.Length; i++)
                {
                    listBox1.Items.Add(f[i]);     //利用for循环添加到列表框中
                }
            }
            else
            {
                MessageBox.Show(this, "没有选择源文件夹！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.ShowDialog();      //浏览文件夹对话框
            textBox2.Text = folderBrowserDialog1.SelectedPath;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox2.Text != "")
            {
                foreach (object o in listBox1.SelectedItems)      //利用foreach循环把源目录中选择的文件复制到目标文件夹中
                {
                    File.Copy(textBox1.Text + "\\" + o.ToString(), textBox2.Text + "\\" + o.ToString());
                }
                int myx = listBox1.SelectedItems.Count;                   //复制文件的个数
                string mystr ="已成功复制"+myx .ToString() +"个文件" ;
                MessageBox.Show(this,mystr  , "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show(this, "目标文件夹不能为空！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
            Application.Exit();
        }
    }
}
