[C#]多檔案複製到資料夾的小程式

  • 23875
  • 0
  • 2010-08-02

多檔案複製到資料夾的小程式

程式的功能是,可以多次的選取要複製的檔案,並把檔案名稱加入ListBox中,當選取複製時,將所有檔案複製到指定的目錄中,參考了msdn中檔案的複製處理以及透過OpenFileDialog獲取要複製的檔案名稱。

以下程式碼

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


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


        // 讀取要複製的檔案的完整路徑與名稱到listbox
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            openFileDialog1.Multiselect = true; // 允許選取多檔案


            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                    foreach (string strFilename in openFileDialog1.FileNames)
                    {
                        listBox1.Items.Add(strFilename);
                    }

            }


        }


        // 複製的按鈕
        private void button2_Click(object sender, EventArgs e)
        {
            string fileName = "";
            string sourcePath = "";
            string targetPath = "";


            if (listBox1.Items.Count > 0)
            {
                foreach (string strFilename in listBox1.Items)
                {
                    // 複製檔案
                    if (listBox1.Items.Count > 0)
                    {
                        int index = strFilename.LastIndexOf("\\");

                        fileName = strFilename.Substring(index+1);
                        sourcePath = strFilename.Substring(0,index);
                        targetPath = FileToBeCopiedTextBox.Text;

                        // Use Path class to manipulate file and directory paths.
                        string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
                        string destFile = System.IO.Path.Combine(targetPath, fileName);

                        // To copy a folder's contents to a new location:
                        // Create a new target folder, if necessary.
                        if (!System.IO.Directory.Exists(targetPath))
                        {
                            System.IO.Directory.CreateDirectory(targetPath);
                        }


                        // To copy a file to another location and  
                        // overwrite the destination file if it already exists.
                        System.IO.File.Copy(sourceFile, destFile, true);
                    }
            
                }

                listBox1.Items.Clear();
                FileToBeCopiedTextBox.Text = "";
                MessageBox.Show("複製成功");
            }


        }


        private void button3_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDialog = new FolderBrowserDialog();

            folderDialog.RootFolder = Environment.SpecialFolder.MyComputer;

            if ((folderDialog.ShowDialog() ==
              System.Windows.Forms.DialogResult.OK))
            {
                this.FileToBeCopiedTextBox.Text = folderDialog.SelectedPath;
            }

        }


        
    }

}

 

執行結果


檔案

FileCopy.rar

參考
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20090219233353JK0&fumcde=
http://msdn.microsoft.com/zh-tw/library/cc148994.aspx
http://msdn.microsoft.com/zh-tw/library/system.windows.forms.openfiledialog.openfile.aspx