0
我試圖從閃存盤複製文件夾到C:
目錄。有時它運行正常,但有時會出現錯誤:運行時錯誤'找不到路徑的一部分'E:
Could not find a part of the path E:\Debug\Vip
您對這個問題有什麼想法嗎?這裏是我的代碼:
string source_dir = "E:\\Debug\\Vip";
string destination_dir = "C:\\Vip";
if (!System.IO.Directory.Exists(destination_dir))
{
System.IO.Directory.CreateDirectory(destination_dir);
}
// substring is to remove destination_dir absolute path (E:\).
// Create subdirectory structure in destination
foreach (string dir in Directory.GetDirectories(source_dir, "*", System.IO.SearchOption.AllDirectories))
{
Directory.CreateDirectory(destination_dir + dir.Substring(source_dir.Length));
}
foreach (string file_name in Directory.GetFiles(source_dir, "*.*", System.IO.SearchOption.AllDirectories))
{
File.Copy(file_name, destination_dir + file_name.Substring(source_dir.Length), true);
}
使用Path.Combine http://msdn.microsoft.com/en-us/library/system .io.path.combine.aspx在創建目錄之前加入兩條路徑。 –