在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例常用C#方法 → C#批量修改文件名

C#批量修改文件名

常用C#方法

下载此实例
  • 开发语言:C#
  • 实例大小:0.40M
  • 下载次数:69
  • 浏览次数:377
  • 发布时间:2019-06-13
  • 实例类别:常用C#方法
  • 发 布 人:2570380570
  • 文件格式:.rar
  • 所需积分:1
 相关标签: C# 文件 修改

实例介绍

【实例简介】

【实例截图】

from clipboard

【核心代码】


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Threading;
namespace FileBatchChangeName
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string[] files;//选择文件的集合
        FileInfo fi;//创建一个FileInfo对象,用于获取文件信息
        string[] lvFiles=new string[7];//向控件中添加的行信息
        Thread td;//处理批量更名方法的线程
        private void 添加文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                listView1.GridLines = true;
                listView1.Items.Clear();
                files = openFileDialog1.FileNames;
                for (int i = 0; i < files.Length; i  )
                {
                    string path = files[i].ToString();
                    fi = new FileInfo(path);
                    string name = path.Substring(path.LastIndexOf("\\")   1, path.Length - 1 - path.LastIndexOf("\\"));
                    string ftype = path.Substring(path.LastIndexOf("."), path.Length - path.LastIndexOf("."));
                    string createTime = fi.CreationTime.ToShortDateString();
                    double a = Convert.ToDouble(Convert.ToDouble(fi.Length) / Convert.ToDouble(1024));
                    string fsize = a.ToString("0.0") " KB";
                    lvFiles[0] = name;
                    lvFiles[1] = name;
                    lvFiles[2] = ftype;
                    lvFiles[3] = createTime;
                    lvFiles[4] = path.Remove(path.LastIndexOf("\\")   1);
                    lvFiles[5] = fsize;

                    ListViewItem lvi = new ListViewItem(lvFiles);
                    lvi.UseItemStyleForSubItems = false;
                    lvi.SubItems[1].BackColor = Color.AliceBlue;
                    
                    listView1.Items.Add(lvi);
                }
                tsslSum.Text = listView1.Items.Count.ToString();
            }
        }


        bool flag = true;
        private void 总在最前ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (flag)
            {
                总在最前ToolStripMenuItem.Checked = true;
                this.TopMost = true;
                flag = false;
            }
            else
            {
                总在最前ToolStripMenuItem.Checked = false;
                this.TopMost = false;
                flag = true;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)//文件名大写
        {
            if (listView1.Items.Count > 0)
            {
                if (radioButton1.Checked)
                {
                    for (int i = 0; i < listView1.Items.Count; i  )
                    {
                        string name = listView1.Items[i].SubItems[1].Text;
                        string name1 = name.Remove(name.LastIndexOf("."));
                        string newName = name.Replace(name1,name1.ToUpper());
                        listView1.Items[i].SubItems[1].Text = newName;
                    }
                }
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)//文件名小写
        {
            if (listView1.Items.Count > 0)
            {
                if (radioButton2.Checked)
                {
                    for (int i = 0; i < listView1.Items.Count; i  )
                    {
                        string name = listView1.Items[i].SubItems[1].Text;
                        string name1 = name.Remove(name.LastIndexOf("."));
                        string newName = name.Replace(name1, name1.ToLower());
                        listView1.Items[i].SubItems[1].Text = newName;
                    }
                }
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)//第一个字母大写
        {
            if (listView1.Items.Count > 0)
            {
                if (radioButton3.Checked)
                {
                    for (int i = 0; i < listView1.Items.Count; i  )
                    {
                        string name = listView1.Items[i].SubItems[1].Text;
                        string name1 = name.Substring(0,1);
                        string name2 = name.Substring(1);
                        string newName = name1.ToUpper()   name2;
                        listView1.Items[i].SubItems[1].Text = newName;
                    }
                }
            }
        }

        private void radioButton4_CheckedChanged(object sender, EventArgs e)//扩展名大写
        {
            if (listView1.Items.Count > 0)
            {
                if (radioButton4.Checked)
                {
                    for (int i = 0; i < listView1.Items.Count; i  )
                    {
                        string name = listView1.Items[i].SubItems[1].Text;
                        string name1 = name.Substring(name.LastIndexOf("."), name.Length - name.LastIndexOf("."));
                        string newName = name.Replace(name1, name1.ToUpper());
                        listView1.Items[i].SubItems[1].Text = newName;
                    }
                }
            }
        }

        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (listView1.Items.Count > 0)
            {
                if (radioButton5.Checked)
                {
                    for (int i = 0; i < listView1.Items.Count; i  )
                    {
                        string name = listView1.Items[i].SubItems[1].Text;
                        string name1 = name.Substring(name.LastIndexOf("."), name.Length - name.LastIndexOf("."));
                        string newName = name.Replace(name1, name1.ToLower());
                        listView1.Items[i].SubItems[1].Text = newName;
                    }
                }
            }
        }

        bool IsOK = false;//判断是否应用了模板
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)//选择模板的下拉框
        {
            int k = (int)nuStart.Value;
            if (comboBox2.Text != "")
            {
                txtTemplate.Text = comboBox2.Text.Remove(comboBox2.Text.LastIndexOf("_"));
                int B = comboBox2.SelectedIndex;
                switch (B)
                {
                    case 0:
                        if (listView1.Items.Count > 0)
                        {
                            for (int i = 0; i < listView1.Items.Count; i  )
                            {
                                string name = listView1.Items[i].SubItems[1].Text;
                                string name1 = name.Remove(name.LastIndexOf("."));
                                string name2 = "pic_"   k.ToString();
                                k = k   (int)nuAdd.Value;
                                string newName = name.Replace(name1, name2);
                                listView1.Items[i].SubItems[1].Text = newName;
                            }
                            IsOK = true;
                        }
                        break;
                    case 1:
                        if (listView1.Items.Count > 0)
                        {
                            for (int i = 0; i < listView1.Items.Count; i  )
                            {
                                string name = listView1.Items[i].SubItems[1].Text;
                                string name1 = name.Remove(name.LastIndexOf("."));
                                string name2 = "file_"   k.ToString();
                                k = k  (int) nuAdd.Value;
                                string newName = name.Replace(name1,name2);
                                listView1.Items[i].SubItems[1].Text = newName;
                            }
                            IsOK = true;
                        }
                        break;
                }
            }
        }

        private void StartNumAndAdd()//设置起始数字和增量值
        {
             int k = (int)nuStart.Value;
             if (comboBox2.Text != "")
             {
                 if (listView1.Items.Count > 0)
                 {
                     for (int i = 0; i < listView1.Items.Count; i  )
                     {
                         string name = listView1.Items[i].SubItems[1].Text;
                         string name1 = name.Remove(name.LastIndexOf("."));
                         string name2 = name1.Remove(name.LastIndexOf("_") 1) k.ToString();
                         k = k   (int)nuAdd.Value;
                         string newName = name.Replace(name1, name2);
                         listView1.Items[i].SubItems[1].Text = newName;
                     }
                     IsOK = true;
                 }
             }
        }


        private void nuStart_ValueChanged(object sender, EventArgs e)//选择起始数字
        {
            StartNumAndAdd();
        }

        private void nuAdd_ValueChanged(object sender, EventArgs e)//选择增量值
        {
            StartNumAndAdd();
        }

        private void txtTemplate_TextChanged(object sender, EventArgs e)//更换模板样式
        {
            if (listView1.Items.Count > 0)
            {
                if (IsOK&&txtTemplate.Text.Trim()!=""&&comboBox2.Text!="")
                {
                    
                    for (int i = 0; i < listView1.Items.Count; i  )
                    {
                        string name = listView1.Items[i].SubItems[1].Text;
                        string name1 = name.Remove(name.LastIndexOf("_")   1);
                        string newName = name.Replace(name1, txtTemplate.Text.Trim()   "_");
                        listView1.Items[i].SubItems[1].Text = newName;
                    }
                }
            }
        }


        private void ChangeName()
        {
            int flag = 0;
            try
            {
                toolStripProgressBar1.Minimum = 0;
                toolStripProgressBar1.Maximum = listView1.Items.Count - 1;
                for (int i = 0; i < listView1.Items.Count; i  )
                {
                    string path = listView1.Items[i].SubItems[4].Text;
                    string sourcePath = path   listView1.Items[i].SubItems[0].Text;
                    string newPath = path   listView1.Items[i].SubItems[1].Text;
                    File.Copy(sourcePath, newPath);
                    File.Delete(sourcePath);
                    toolStripProgressBar1.Value = i;
                    listView1.Items[i].SubItems[0].Text = listView1.Items[i].SubItems[1].Text;
                    listView1.Items[i].SubItems[6].Text = "√成功";
                }
            }
            catch(Exception ex)
            {
                flag  ;
                MessageBox.Show(ex.Message);
            }
            finally
            {
                tsslError.Text = flag.ToString()   " 个错误";
            }
        }

        private void 更名ToolStripMenuItem_Click(object sender, EventArgs e)//开始批量更名
        {
            if (listView1.Items.Count > 0)
            {
                for (int i = 0; i < listView1.Items.Count; i  )
                {
                    listView1.Items[i].SubItems[6].Text = "";
                }
                tsslError.Text = "";
                td = new Thread(new ThreadStart(ChangeName));
                td.Start();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
        }

        private void 导出文件列表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw;
                string txt = "";
                string path = saveFileDialog1.FileName;
                for (int i = 0; i < listView1.Items.Count; i  )
                {
                    txt = listView1.Items[i].SubItems[0].Text   "  "   listView1.Items[i].SubItems[1].Text;
                    sw = File.AppendText(path);
                    sw.WriteLine(txt);
                    sw.Close();
                }
            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private static string TraditionalChineseToSimplifiedChinese(string str)//繁体转简体
        {
            return (Microsoft.VisualBasic.Strings.StrConv(str,Microsoft.VisualBasic.VbStrConv.SimplifiedChinese,0));
        }

        private static string SimplifiedChineseToTraditionalChinese(string str)//简体转繁体
        {
            return (Microsoft.VisualBasic.Strings.StrConv(str as string ,Microsoft.VisualBasic.VbStrConv.TraditionalChinese,0));
        }

        private void 繁体转简体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.Items.Count > 0)
            {
                for (int i = 0; i < listView1.Items.Count; i  )
                {
                    string name = listView1.Items[i].SubItems[1].Text;
                    string name1 = TraditionalChineseToSimplifiedChinese(name);
                    listView1.Items[i].SubItems[1].Text = name1;
                }
            }
        }

        private void 简体转繁体ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listView1.Items.Count > 0)
            {
                for (int i = 0; i < listView1.Items.Count; i  )
                {
                    string name = listView1.Items[i].SubItems[1].Text;
                    string name1 = SimplifiedChineseToTraditionalChinese(name);
                    listView1.Items[i].SubItems[1].Text = name1;
                }
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (td != null)
            {
                td.Abort();
            }
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 ab = new AboutBox1();
            ab.ShowDialog();
        }
    }
}


标签: C# 文件 修改

实例下载地址

C#批量修改文件名

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警