zoukankan      html  css  js  c++  java
  • C#文件复制小工具

    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) 
    
  • 相关阅读:
    Jenkins理解逻辑图
    什么是Jenkins?
    SpringBoot Test及注解详解
    如何熟悉一个新项目
    调用百度OCR模块进行文字识别
    python安装包的方法&安装遇到的问题总结_2020_11_19
    怎么让谷歌浏览器记住密码(不需要任何插件)
    excel以一列数据为x一列为y作折线图
    java创建新java文件的方法
    Mathematics释放变量的方法
  • 原文地址:https://www.cnblogs.com/strivers/p/1951712.html
Copyright © 2011-2022 走看看