C# 檔案批量重新命名工具源碼執行個體

來源:互聯網
上載者:User

C# 檔案批量重新命名工具源碼執行個體 

 

如有轉載請註明出處:http://www.cnblogs.com/flydoos/archive/2011/10/29/2228300.html

C# 檔案批量重新命名工具源碼執行個體 

 

今天,群裡有人問了一個關於批量重新命名的問題,所以就寫個Demo,需要的也來看看吧。沒難度,直接貼源碼,拋磚引玉,並且附上VS2010項目源碼附件。寫得不好的地方,希望大家指出來,以便日後改正。 (核心只是File.Move() ,呵呵)

 

本人正在學習WPF,誰有關於WPF製作電子書閱讀程式、匯出/匯入 PDF、Excel、WORD 之類的資料,希望發給我一份,謝謝。郵箱:flydoos@vip.qq.com

 

//////////////////////////////////////////////////////////////////

//作者:牛A與牛C之間
//Q Q:1046559384   C#/Java技術交流群:96020642
//微博:http://weibo.com/flydoos

//部落格:http://www.cnblogs.com/flydoos

//日期:2011-10-29

//////////////////////////////////////////////////////////////////
using System;
using System.Windows.Forms;
using System.IO;

namespace FileRename
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void btnOpenPath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog f1 = new FolderBrowserDialog();
            if (f1.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = f1.SelectedPath;
            }
        }

        private void btnRename_Click(object sender, EventArgs e)
        {
            if (txtPath.Text != "")
            {
                if (txtNew.Text != "")
                {
                    string strOldPart = txtOld.Text.Trim();
                    string strNewPart = txtNew.Text.Trim();
                    DateTime StartTime = DateTime.Now;   
                    try
                    {
                        DirectoryInfo di = new DirectoryInfo(txtPath.Text);
                        FileInfo[] filelist = di.GetFiles("*.*");
                        string strFileFolder = txtPath.Text;
                        int i = 0;
                        int TotalFiles = 0;
                        foreach (FileInfo fi in filelist)
                        {
                            string strOldFileName = fi.Name;
                            string strNewFileName = fi.Name.Replace(strOldPart, strNewPart);
                            string strNewFilePath = @strFileFolder + "\\" + strNewFileName;
                            filelist[i].MoveTo(@strNewFilePath);
                            TotalFiles += 1;
                            lstFiles.Items.Add("檔案名稱:" + strOldFileName + "    已重新命名為  " + strNewFileName + "");
                            i += 1;
                        }
                        DateTime EndTime = DateTime.Now;
                        TimeSpan ts = EndTime - StartTime;
                        Text = ("總耗時:" + ts.Hours + "時" + ts.Minutes + "分" + ts.Seconds + "秒" + ts.Milliseconds + "毫秒");
                    }
                    catch
                    {
                        MessageBox.Show("路徑無效!");
                    }
                }
                else
                {
                    MessageBox.Show("沒有匹配字元");
                }
            }
            else
            {
                MessageBox.Show("請先擇擇路徑!");
            }
        }
    }
}

 

 

完整代碼下載:網盤下載1  網盤下載2  備用下載3

 

VS2010的項目,如果無法開啟,請到這裡下載 Visual Studio 版本互轉工具1.1 ,之後就能開啟了

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.