此問題曾請求過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」
謝謝你的幫助!
此問題曾請求過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」
謝謝你的幫助!
創建表示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);
}
}
謝謝!有用 :) – cethint 2012-03-07 22:20:14
「重命名」文件是這樣的:
而且你可以在每個文件名做filename.Replace('_', '-')
用連字符來代替下劃線。
好的。我看到了這個。但我不想一次又一次地寫文件路徑。我想在路徑「C:\ Users \ dell \ Desktop \ rename」中更改所有文件的名稱。 – cethint 2012-03-07 22:10:57
有什麼問題嗎?你有什麼嘗試? – 2012-03-07 22:04:41
你已經試過了什麼? – 2012-03-07 22:05:03
我是新來的c#。我剛剛在PowerShell中嘗試過。不過,我想用C#來做到這一點。 – cethint 2012-03-07 22:07:12