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 copyFile{ public partial class Form1 : Form { String fileName; String folderName; String extendedName; String fileName1; public Form1() { InitializeComponent(); } private void browse_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); //new一個方法 ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //定義開啟的預設資料夾位置 ofd.ShowDialog(); //顯示開啟檔案的視窗 fileName = ofd.FileName; //獲得選擇的檔案路徑 textBox1.Text = fileName; extendedName = Path.GetExtension(fileName); //獲得副檔名 fileName1 = Path.GetFileName(fileName); //獲得檔案名稱 } private void folder_Click(object sender, EventArgs e) { FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.ShowDialog(); folderName = fbd.SelectedPath; //獲得選擇的檔案夾路徑 textBox3.Text = folderName; } private void ok_Click(object sender, EventArgs e) { if (textBox1.Text.Trim().Length == 0) { MessageBox.Show("檔案路徑不可為空!"); return; } if (textBox2.Text.Trim().Length == 0) { MessageBox.Show("複製數量不可為空!"); return; } if (textBox3.Text.Trim().Length == 0) { MessageBox.Show("目標檔案夾路徑不可為空!"); return; } String newFile; //定義儲存的位置,和儲存的名稱 for (int i = 1; i <= Convert.ToInt32(textBox2.Text); i++) //從textBox2中擷取要複製的次數 { newFile = folderName + "\\" + fileName1 +"_"+ i.ToString() + extendedName; File.Copy(fileName, newFile, true); //使用Copy複製檔案, Copy(源檔案位置,目標檔案夾位置,是否可以覆蓋同名檔案) } MessageBox.Show("複製完成!"); } }}
//擷取檔案名稱 Path.GetFileName(OpenFileDialog.FileName) //擷取檔案路徑 Path.GetDirectoryName(OpenFileDialog.FileName) //擷取副檔名 Path.GetExtension(OpenFileDialog.FileName)