基本信息
源码名称:C# 批量重命名文件 实例源码下载
源码大小:0.04M
文件格式:.rar
开发语言:C#
更新时间:2013-09-20
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

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("请先择择路径!");
            }
        }
    }
}