用C# 做的一個小而實用的 工具

來源:互聯網
上載者:User

當初因為要用到這種功能的工具,便從網上找了C# 重新命名檔案的方法,便自己做了一個。雖然簡單,但挺實用的,我經常能用到

 

 

就一個檔案代碼:

代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//C#批量重新命名檔案代碼的實現
//添加檔案操作空間引用
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;//擷取結束時間
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("請先擇擇路徑!");
}
}
}
}

 

相關文章

聯繫我們

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