C#檔案複製小工具

來源:互聯網
上載者:User
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) 
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.