我有像C:\ Users \ XXX \ Desktop \ Test \ DUT3_Mode1A \ DUT3_Mode1B \ DUT3_Mode2这样的文件夹。
我将浏览文件夹到Test。我需要更改如果名称包含Mode1A然后我只是重命名为M1A像不同的条件与名称。例如我有路径像DUT3_Mode1A \ DUT3_Mode1B \ DUT3_Mode2我需要重命名DUT3_M1A \ DUT3_M1B \ DUT3_M2
请参阅下面的示例,例如我需要更改文件夹结构。请跟我说我是新加入者。在此先感谢
M1A = Mode1A
M1B = Mode1B
M2 = Mode2
M3 = Mode3
M6 = Mode6
Read = Read DTCs
Clear = Clear DTCs
EPB = EPB_APPL-REL
PWRD = POWER_DOWN
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;
namespace FolderStructure
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
string strpath = string.Empty;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "Custom Description";
if (fbd.ShowDialog() == DialogResult.OK)
{
string sSelectedPath = fbd.SelectedPath;
textBox1.Text = sSelectedPath;
strpath = sSelectedPath;
}
}
private void button2_Click(object sender, EventArgs e)
{
ChangeFileName(strpath);
}
public void ChangeFileName(string folderPath)
{
string strtemp = string.Empty;
////string[] folders = folderPath.Split(System.IO.Directory.GetDirectories(folderPath, "*", System.IO.SearchOption.AllDirectories););
string[] folders = System.IO.Directory.GetDirectories(folderPath, "*", System.IO.SearchOption.AllDirectories);
DirectoryInfo info = new DirectoryInfo(folderPath);
foreach (string path in folders)
{
//code needed
}
}
}
}
答案 0 :(得分:2)
此方法将重命名给定路径中的所有目录和子目录,将'from'字符串替换为'to'字符串:
public static void Replace(string path, string from, string to)
{
string prefix = path;
string[] folders = System.IO.Directory.GetDirectories(path, "*", System.IO.SearchOption.TopDirectoryOnly);
foreach (string folder in folders)
{
// recursively rename all subfolders first
Replace(folder, from, to);
string newFolderName = prefix + folder.Substring(prefix.Length).Replace(from, to);
if (newFolderName != folder)
System.IO.Directory.Move(folder, newFolderName);
}
}
用法
Replace(folderPath, "Mode", "M");
编辑:您确定要将难度级别更改为简单吗?
private static Dictionary<string, string> replacements = new Dictionary<string, string>
{
{ "Mode1A", "M1A" },
{ "Mode1B", "M1B" },
{ "Mode1", "M1" },
{ "Mode2", "M2" },
{ "Mode3", "M3" },
{ "Mode6", "M6" },
{ "Read DTCs", "Read" },
{ "Clear DTCs", "Clear" },
{ "EPB_APPL-REL", "EPB" },
{ "POWER_DOWN", "PWRD" }
};
private void button2_Click(object sender, EventArgs e)
{
ChangeFolderNames();
}
private void ChangeFolderNames()
{
foreach(KeyValuePair<string, string> pair in replacements)
Replace(strpath, pair.Key, pair.Value);
}
public static void Replace(string path, string from, string to)
{
string prefix = path;
string[] folders = System.IO.Directory.GetDirectories(path, "*", System.IO.SearchOption.TopDirectoryOnly);
foreach (string folder in folders)
{
// recursively rename all subfolders first
Replace(folder, from, to);
string newFolderName = prefix + folder.Substring(prefix.Length).Replace(from, to);
if (newFolderName != folder)
System.IO.Directory.Move(folder, newFolderName);
}
}
答案 1 :(得分:0)
您可以修改下面的递归方法来重命名文件。下面的代码将所有文件名写入xml格式。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace SAveDirectoriesXml
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
const string FOLDER = @"c:\temp";
static XmlWriter writer = null;
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
writer = XmlWriter.Create(FILENAME, settings);
writer.WriteStartDocument(true);
DirectoryInfo info = new DirectoryInfo(FOLDER);
WriteTree(info);
writer.WriteEndDocument();
writer.Flush();
writer.Close();
Console.WriteLine("Enter Return");
Console.ReadLine();
}
static long WriteTree(DirectoryInfo info)
{
long size = 0;
writer.WriteStartElement("Folder");
try
{
writer.WriteAttributeString("name", info.Name);
writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
foreach (DirectoryInfo childInfo in info.GetDirectories())
{
size += WriteTree(childInfo);
}
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
FileInfo[] fileInfo = null;
try
{
fileInfo = info.GetFiles();
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error",errorMsg);
}
if (fileInfo != null)
{
foreach (FileInfo finfo in fileInfo)
{
try
{
writer.WriteStartElement("File");
writer.WriteAttributeString("name", finfo.Name);
writer.WriteAttributeString("size", finfo.Length.ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
writer.WriteEndElement();
size += finfo.Length;
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
}
}
writer.WriteElementString("size", size.ToString());
writer.WriteEndElement();
return size;
}
}
}