【发布时间】:2018-03-13 17:59:36
【问题描述】:
我有如下代码(其实是分了各种方法,不过是这样的):
string ThePath = FBD.SelectedPath; // FBD is a FolderBrowserDialog.
string TheSubDirPath = Path.Combine(ThePath, TheSubDirName);
if (Directory.Exists(TheSubDirPath)) { Directory.Delete(TheSubDirPath, true); } // Want a clean, empty directory.
Directory.CreateDirectory(TheSubDirPath);
string TheSrcFileName = Path.Combine(ThePath, MyOldFileName);
string TheDestFileName = Path.Combine(TheSubDirPath, MyNewFileName);
File.Copy(TheSrcFileName, TheDestFileName, false); // Overwriting is impossible, so not needed.
最后一行导致带有消息的 DirectoryNotFoundException
找不到路径“C:\Users...\Test01\TheSubDirName\MyNewFileName”的一部分。”
源路径和目标路径都正是我想要的。 我尝试在目录删除后和目录创建后插入延迟,但没有效果。我有一个堆栈跟踪,它显示了问题的核心
在 System.IO.Error.WinIOError(Int32 errorCode, String maybeFullPath)
在 System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
在 System.IO.File.Copy(String sourceFileName, String destFileName, Boolean overwrite)
有什么想法吗?
【问题讨论】:
-
TheSrcFileName和TheDestFileName的值是多少? -
您的代码对我来说可以正常工作,即使
c:\users...\dunsany中有三个点也是如此。请分享足够的信息,以便我们重现该问题。 -
如果
TheSubDirPath已经作为目录以外的东西存在怎么办? -
TheSrcFileName 是 "C:\Users\MyName\Documents\Test01\filename.ext"
-
TheDestFileName 是 C:\Users\MyName\Documents\Test01\subdirname\anotherfilename.ext"