2012-03-07 49 views
2

此问题曾请求过PowerShell。我想在C#中做到这一点。 我的桌面上有一个目录。目录名称是“重命名”。在C中替换所有directiories和文件的名称#

C:\Users\dell\Desktop\rename 

并且“重命名”文件夹包含“a_b”,“b_c”,“c_d”,“d_e”文件夹。 我想用“ - ”字符替换“_”。 换句话说,文件夹的新名称将是“a-b”,“b-c”,“c-d”,“d-e”

谢谢你的帮助!

+0

有什么问题吗?你有什么尝试? – 2012-03-07 22:04:41

+4

你已经试过了什么? – 2012-03-07 22:05:03

+0

我是新来的c#。我刚刚在PowerShell中尝试过。不过,我想用C#来做到这一点。 – cethint 2012-03-07 22:07:12

回答

6

创建表示rename文件夹DirectoryInfo对象,通过其子文件夹重复,并使用String.Replace更换_-

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

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

    if(newName != child.FullPath) { 
     child.MoveTo(newName); 
    } 
} 
+0

谢谢!有用 :) – cethint 2012-03-07 22:20:14

相关问题