[英]Why is File.Move not working as expected?
我正在嘗試將所有文件從rootFolderPath
移到destinationPath
try
{
string rootFolderPath = @"D:\Log_siteq\";
if (!Directory.Exists(Path.Combine(@"D:\Log_takaya\" + comboBox1.SelectedItem.ToString())))
{
System.IO.Directory.CreateDirectory(Path.Combine(@"D:\Log_takaya\" + comboBox1.SelectedItem.ToString()));
}
string destinationPath = Path.Combine(@"D:\Log_takaya\" + comboBox1.SelectedItem.ToString() );
string fileTypes = @"*.*";
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, fileTypes);
foreach (string file in fileList)
{
string ext = Path.GetExtension(file);
string destination = Path.Combine(destinationPath,file);
File.Move( file,destination);
MessageBox.Show(file);
MessageBox.Show(destination);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
顯然是MessageBox.Show(file);
顯示我的根文件夾路徑(正常情況下),但顯示MessageBox.Show(destination);
向我展示了同樣的東西。
是什么賦予了? 它只是將我的file
從我的根文件夾移到了同一文件夾中。
您正在合並destinationPath
用的完整文件路徑file
:
string destination = Path.Combine(destinationPath, file);
只會使用原始文件路徑覆蓋目標位置(因為“ C:\\ desitnation \\ C:\\ source \\ filename.txt”不是有效路徑)。
相反,您只需要這樣的文件名:
string destination = Path.Combine(destinationPath, Path.GetFileName(file));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.