C#文件复制小工具

    技术2025-06-17  13

    http://www.cnblogs.com/strivers/archive/2011/02/11/1951712.html

     

    01using System;

    02using System.Collections.Generic; 03using System.ComponentModel; 04using System.Data; 05using System.Drawing; 06using System.Text; 07using System.Windows.Forms; 08using System.IO; 09   10namespace copyFile 11{ 12    public partial class Form1 : Form 13    { 14        String fileName; 15        String folderName; 16        String extendedName; 17        String fileName1; 18           19        public Form1() 20        { 21            InitializeComponent(); 22        } 23   24        private void browse_Click(object sender, EventArgs e) 25        { 26            OpenFileDialog ofd = new OpenFileDialog();                //new一个方法 27            ofd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);  //定义打开的默认文件夹位置 28            ofd.ShowDialog();          //显示打开文件的窗口 29             fileName = ofd.FileName;               //获得选择的文件路径 30             textBox1.Text = fileName; 31             extendedName = Path.GetExtension(fileName);       //获得文件扩展名 32             fileName1 = Path.GetFileName(fileName);           //获得文件名 33        } 34   35        private void folder_Click(object sender, EventArgs e) 36        { 37            FolderBrowserDialog fbd = new FolderBrowserDialog(); 38            fbd.ShowDialog(); 39            folderName = fbd.SelectedPath;                     //获得选择的文件夹路径 40            textBox3.Text = folderName; 41        } 42   43        private void ok_Click(object sender, EventArgs e) 44        { 45            if (textBox1.Text.Trim().Length == 0) 46            { 47                MessageBox.Show("文件路径不能为空!"); 48                return; 49            } 50            if (textBox2.Text.Trim().Length == 0) 51            { 52                MessageBox.Show("复制数量不能为空!"); 53                return; 54            } 55            if (textBox3.Text.Trim().Length == 0) 56            { 57                MessageBox.Show("目标文件夹路径不能为空!"); 58                return; 59            } 60            String newFile;                   //定义存储的位置,和存储的名称 61               62            for (int i = 1; i <= Convert.ToInt32(textBox2.Text); i++)                   //从textBox2中获取要复制的次数 63            { 64                newFile = folderName + "//" + fileName1 +"_"+ i.ToString() + extendedName; 65                File.Copy(fileName, newFile, true);            //使用Copy复制文件, Copy(源文件位置,目标文件夹位置,是否可以覆盖同名文件) 66            } 67            MessageBox.Show("复制完成!"); 68        } 69    } 70} view source print ? 1//获取文件名  2Path.GetFileName(OpenFileDialog.FileName)  3   4//获取文件路径  5Path.GetDirectoryName(OpenFileDialog.FileName)  6   7//获取文件扩展名  8Path.GetExtension(OpenFileDialog.FileName)
    最新回复(0)