C# 为什么File.Move不能按预期工作?
我正在尝试将所有文件从C# 为什么File.Move不能按预期工作?,c#,C#,我正在尝试将所有文件从rootFolderPath移动到destinationPath try { string rootFolderPath = @"D:\Log_siteq\"; if (!Directory.Exists(Path.Combine(@"D:\Log_takaya\" + comboBox1.SelectedItem.ToString()))) { System.IO.Directory.CreateDirectory(Path.Com
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(文件)代码>显示我的根文件夹路径(正常情况下),但MessageBox.Show(目标)代码>正在向我显示相同的内容
有什么好处?它只是将我的文件
从同一文件夹中的根文件夹中移动。我没有得到什么吗?您正在将目标路径
与文件
的完整文件路径相结合:
string destination = Path.Combine(destinationPath, file);
这将用原始文件路径覆盖目标(因为“C:\desitnation\C:\source\filename.txt”不是有效路径)
相反,您只需要这样的文件名:
string destination = Path.Combine(destinationPath, Path.GetFileName(file));
不要使用路径。像这样组合。不要使用+组合路径。像这样使用:Path.Combine(“D:\Log_takaya”,comboBox1.SelectedItem.ToString());我没看到,但问题依然存在,没有任何改变。