新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用C#如何批量重命名文件
這篇文章將為大家詳細講解有關使用C#如何批量重命名文件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
成都創(chuàng)新互聯(lián)企業(yè)建站,十載網(wǎng)站建設經(jīng)驗,專注于網(wǎng)站建設技術,精于網(wǎng)頁設計,有多年建站和網(wǎng)站代運營經(jīng)驗,設計師為客戶打造網(wǎng)絡企業(yè)風格,提供周到的建站售前咨詢和貼心的售后服務。對于成都網(wǎng)站建設、成都做網(wǎng)站中不同領域進行深入了解和探索,創(chuàng)新互聯(lián)在網(wǎng)站建設中充分了解客戶行業(yè)的需求,以靈動的思維在網(wǎng)頁中充分展現(xiàn),通過對客戶行業(yè)精準市場調(diào)研,為客戶提供的解決方案。
具體如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; //C#批量重命名文件代碼的實現(xiàn) //添加文件操作空間引用 using System.IO; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { FolderBrowserDialog f1 = new FolderBrowserDialog(); if (f1.ShowDialog() == DialogResult.OK) { textBox3.Text = f1.SelectedPath; } } private void button2_Click(object sender, EventArgs e) { if (textBox3.Text!=""){ if(textBox1.Text!="") { string strOldFileName; string strNewFileName; string strOldPart = this.textBox1.Text.Trim(); string strNewPart = this.textBox2.Text.Trim(); string strNewFilePath; string strFileFolder; int TotalFiles = 0; DateTime StartTime = DateTime.Now;//獲取開始時間 try{ DirectoryInfo di = new DirectoryInfo(textBox3.Text); FileInfo[] filelist = di.GetFiles("*.*"); strFileFolder = textBox3.Text; int i = 0; foreach (FileInfo fi in filelist) { strOldFileName = fi.Name; strNewFileName = fi.Name.Replace(strOldPart, strNewPart); strNewFilePath = @strFileFolder + "\\" + strNewFileName; filelist[i].MoveTo(@strNewFilePath); TotalFiles += 1; this.listBox1.Items.Add("文件名:" + strOldFileName + " 已重命名為 " + strNewFileName + ""); i += 1; } DateTime EndTime = DateTime.Now;//獲取結(jié)束時間 TimeSpan ts = EndTime - StartTime; this.listBox1.Items.Add("總耗時:" + ts.Hours.ToString() + "時" + ts.Minutes.ToString() + "分" + ts.Seconds.ToString() + "秒"+ ts.Milliseconds.ToString()+"毫秒"); } catch { MessageBox.Show("路徑無效!"); } } else { MessageBox.Show("沒有匹配字符"); } } else { MessageBox.Show("請先擇擇路徑!"); } } } }
關于使用C#如何批量重命名文件就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)頁標題:使用C#如何批量重命名文件
鏈接分享:http://fisionsoft.com.cn/article/jsjsoc.html