C# 文件批量重命名工具源码实例

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 ,之后就能打开了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值