C# 修改文件名 demo

来源:互联网 发布:留学存款证明 知乎 编辑:程序博客网 时间:2024/06/16 08:47
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;using System.Collections;namespace changeFileName{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        private void button1_Click(object sender, EventArgs e)        {            FolderBrowserDialog dialog = new FolderBrowserDialog();            dialog.Description = "请选择文件路径";            if (dialog.ShowDialog() == DialogResult.OK)            {                try                {                    string folderPath = dialog.SelectedPath;                    textBox1.Text = folderPath;                    ArrayList allFileList = getAllFile(folderPath);//获取所有文件                    string fileName;//文件名                    string suffix;//文件后缀                    string newFileName;//新文件名                    string result = "";                    foreach (string file in allFileList)                    {                        fileName = getFileName(file);                        suffix = getFileSuffix(file);                        newFileName = formatStr(fileName, 6, '0');                        File.Move(file, file.Replace(fileName + '.' + suffix, newFileName + '.' + suffix));//修改                        result += file                            + "\n修改为: " + file.Replace(fileName + '.' + suffix, newFileName + '.' + suffix)                            + "\n修改成功\n\n";                    }                    richTextBox1.Text = result;                    MessageBox.Show("修改成功!");                }                catch(Exception err)                {                    MessageBox.Show(err.Message);                }            }        }        //获取指定路径下的文件        public ArrayList getFile(string path)        {            ArrayList fileList = new ArrayList();            if (Directory.Exists(path))            {                fileList.AddRange(Directory.GetFiles(path));            }            return fileList;        }        //获取指定路径下的文件夹(包含子文件夹)        public ArrayList allFileList = new ArrayList();        public ArrayList getFolder(string path)        {            string[] folderList = Directory.GetDirectories(path);            if (folderList.Length > 0)            {                foreach (string folder in folderList)                {                    allFileList.Add(folder);                    getFolder(folder);                }            }            return allFileList;        }        //获取所有文件        public ArrayList getAllFile(string path)        {            ArrayList fileList = new ArrayList();            foreach (string filePath in getFile(path))//先获取当前路径的文件            {                fileList.Add(filePath.Replace(@"\", @"/"));            }            foreach (string folderPath in getFolder(path))//获取所有文件夹            {                foreach (string filePath in getFile(folderPath))//获取对应文件夹中的文件                {                    fileList.Add(filePath.Replace(@"\", @"/"));                }            }            return fileList;        }        //获取文件名(不带后缀)        public string getFileName(string path)        {            int position1 = path.LastIndexOf(".");//最后一个 .            int position2 = path.LastIndexOf("/");//最后一个 /            return path.Substring(position2 + 1, position1 - position2 - 1);        }        //获取后缀        public string getFileSuffix(string path)        {            int position = path.LastIndexOf(".");//最后一个 /            return path.Substring(position + 1);        }        //补位格式化        public string formatStr(string str, int len, char symbol)        {            int flag;            int index = -1;            if (str.Length < len)            {                for (int i = 0; i < str.Length; i++)                {                    if (int.TryParse(str[i].ToString(), out flag))//数字                    {                        index = i;                        break;                    }                }                if (index != -1)                {                    return str.Substring(0, index) +                        str.Substring(index).PadLeft(len - index, symbol);                }            }            return str;        }    }}

0 0