替换C#中所有directiories和文件的名称

cet*_*int 3 c# directory filenames replace

这个问题以前曾要求PowerShell.我想在C#中这样做.我的桌面上有一个目录.目录名称是"重命名".

C:\Users\dell\Desktop\rename
Run Code Online (Sandbox Code Playgroud)

并且"重命名"文件夹包含"a_b","b_c","c_d","d_e"文件夹.我想用" - "字符替换"_".换句话说,文件夹的新名称将是"ab","bc","cd","de"

谢谢您的帮助!

Ry-*_*Ry- 9

创建一个DirectoryInfo表示该rename文件夹的对象,遍历其子文件夹,并使用String.Replace替换_为-:

var parent = new DirectoryInfo(@"C:\Users\dell\Desktop\rename");

foreach (var child in parent.GetDirectories()) {
    var newName = child.FullPath.Replace('_', '-');

    if (newName != child.FullPath) {
        child.MoveTo(newName);
    }
}
Run Code Online (Sandbox Code Playgroud)