需要将待修改的Json文件放在.exe同一级目录下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FileReName
{
public partial class Form1 : Form
{
//String defaultfilepath;
public Form1()
{
InitializeComponent();
}
//string path = Application.StartupPath;
int fileNumber = 0;
//DirectoryInfo myDirectoryInfo = new DirectoryInfo(path);
//string[] files = Directory.GetFiles(path, "*.json");
//string str = Path.GetFullPath(path);
//str = Path.GetExtension(filePath); //-->.xml(获取后缀扩展名称)
//str = "获取文件的名称含有后缀:" + Path.GetFileName(filePath); //-->BenXHCMS.xml
//FileInfo[] myFileInfoArray = myDirectoryInfo.GetFiles();
//foreach (var item in myFileInfoArray)
//{
//}
private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
string filePath = folderBrowserDialog1.SelectedPath; //获取所选路径
string[] files = Directory.GetFiles(filePath, "*.json"); //获取json全路径
//string filesPath = "C:\\JiYF\\BenXH\\BenXHCMS.xml";
//string fileName= Path.GetExtension(filesPath); //-->.xml(获取后缀扩展名称)
textBox1.Text = filePath; //打印所选文件路径
int i = 0;
foreach (var file in files) //遍历所有json文件并获取绝对路径
{
fileNumber = files.Length; //打印指定文件数量
i += 1;
//从绝对路径数组文件名获取各文件名
string fileNames = Path.GetFileName(file); //获取文件名,无后缀
//对文件名进行修改
//File.Move(fileName, newFileName); //Move函数参数(原始文件名,目标文件名);
string str = "0000";
str = str + i.ToString();
File.Move(fileNames, str + ".json");
}
textBox2.Text = fileNumber.ToString();
}
MessageBox.Show("文件转换已完成!!!");
}
}
}
WinForm界面如下