2012-03-07 41 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

相關問題