资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用C#如何批量重命名文件-创新互联

这篇文章将为大家详细讲解有关使用C#如何批量重命名文件,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

成都创新互联公司是一家专业提供合山企业网站建设,专注与成都网站建设、做网站、H5响应式网站、小程序制作等业务。10年已为合山众多企业、政府机构等服务。创新互联专业网络公司优惠进行中。

具体如下:

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

关于使用C#如何批量重命名文件就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章题目:使用C#如何批量重命名文件-创新互联
网站网址:http://cdkjz.cn/article/doiocg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220