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
- C# 修改文件名 demo
- C#修改文件名方法
- 使用C#修改文件名
- C#批量修改文件名
- C#之批量修改文件名
- C#批量修改文件名的核心代码
- 修改文件名
- 修改文件名
- 修改文件名
- 修改文件名
- 修改文件名
- 修改文件名
- 修改文件名
- 批量文件名修改
- VB.NET修改文件名
- 修改数据库逻辑文件名
- 批量修改文件名
- 批量修改文件名
- HDU 4445 - Crazy Tank(暴力枚举)
- 第14周项目6-6-二维数组斜对称相等数筛选
- js Ajax检测用户名是否被占用
- 碰到困难,心情有点沉重
- linux 上卸载Oracle
- C# 修改文件名 demo
- 判断字符串是不是回文,使用C++、Python两种语言
- linux安装jdk
- Word模板设计时,如何让照片填满单元格
- UnicodeEncodeError: 'ascii' codec can't encode characters in position
- 编程:二维数组的操作
- 第十四周 项目三 多个成绩的成绩单
- find查找多种文件后缀
- iOS 获取界面的第一响应者