我想使用C#.NET將目錄從一個位置移動到另一個位置。我用Directory.Move
甚至DirectoryInfo的(與通過MoveTo)這個簡單的方法:將文件夾(目錄)從一個位置移動到另一個位置 - 行爲不端
// source is: "C:\Songs\Elvis my Man"
// newLocation is: "C:\Songs\Elvis"
try
{
// Previous command was: Directory.Move(source, newLocation);
DirectoryInfo dir = new DirectoryInfo(source);
dir.MoveTo(newLocation);
}
catch (Exception e)
{
Console.WriteLine("Error: "+ e.Message);
}
但行動應該正在做(這兩種情況下)被重命名的文件夾名稱從「源」到「newLocation」
我的預期是什麼?那個文件夾「Elvis my man」現在會在「Elvis」文件夾中。
發生了什麼事?「貓王我的男人」改爲「貓王」(改名)。如果目錄「Elvis」已經存在,它不能將其更改爲「Elvis」(因爲他不能重複名稱),所以我得到一個異常說。
我在做什麼錯?
非常感謝!