C#文件复制小工具

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}
1//获取文件名 
2Path.GetFileName(OpenFileDialog.FileName) 
3  
4//获取文件路径 
5Path.GetDirectoryName(OpenFileDialog.FileName) 
6  
7//获取文件扩展名 
8Path.GetExtension(OpenFileDialog.FileName)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值