File.Move 的 System.IO.DirectoryNotFoundException

发布于 2024-11-29 09:20:51 字数 1069 浏览 1 评论 0原文

只是一个简单的问题(我希望如此): 当我使用 File.Move 时,它​​给了我一个错误:

System.IO.DirectoryNotFoundException was unhandled by user code
  Message=Could not find a part of the path.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.__Error.WinIOError()
       at System.IO.File.Move(String sourceFileName, String destFileName)
       at Portal_2_Level_Installer.Form1.WorkMagic(String FileLocation) in C:\Users\Yoshie\Local Settings\Documents\Visual Studio 2010\Projects\Portal 2 Level Installer\Portal 2 Level Installer\Form1.cs:line 265
  InnerException: 

我的代码:

File.Move(FileLocation, destinationPath);

以及变量的内容:

destinationPath="c:/program files (x86)/steam\\steamapps\\common\\portal 2\\Test\\Test.docx"
FileLocation="C:\\Users\\Yoshie\\Local Settings\\Documents\\Test.docx"

谢谢! 编辑:我现在真的觉得自己像个白痴。我没有意识到目标文件夹必须存在!我愚蠢地认为如果目标文件夹尚不存在,就会自动创建它。抱歉浪费了您的时间,但还是感谢您的回答! (我现在知道我可以使用@来停止转义,所以很高兴知道) 不管怎样,还是谢谢你,再次抱歉!

Just a quick question (I hope):
When I use File.Move it gives me an error:

System.IO.DirectoryNotFoundException was unhandled by user code
  Message=Could not find a part of the path.
  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.__Error.WinIOError()
       at System.IO.File.Move(String sourceFileName, String destFileName)
       at Portal_2_Level_Installer.Form1.WorkMagic(String FileLocation) in C:\Users\Yoshie\Local Settings\Documents\Visual Studio 2010\Projects\Portal 2 Level Installer\Portal 2 Level Installer\Form1.cs:line 265
  InnerException: 

My code:

File.Move(FileLocation, destinationPath);

And the contents of the variables:

destinationPath="c:/program files (x86)/steam\\steamapps\\common\\portal 2\\Test\\Test.docx"
FileLocation="C:\\Users\\Yoshie\\Local Settings\\Documents\\Test.docx"

Thanks!
EDIT: I really feel like an idiot now. I didn't realise that the destination folder had to exist! I stupidly assumed that the destination folder would be automatically created if it didn't already exist. Sorry for wasting your time, but thanks for the answers anyway! (I now know that I can use @ to stop escaping, so thats good to know)
Thanks anyway, and again, sorry!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遥远的她 2024-12-06 09:20:51

请使用 \ 而不是 / 以及使用 @,如 @"path"。

Please use \ and not / as well as use @ like @"path".

空城之時有危險 2024-12-06 09:20:51

这有什么区别吗?

destinationPath=@"c:\program files (x86)\steam\steamapps\common\portal 2\Test\Test.docx";
FileLocation=@"C:\Users\Yoshie\Local Settings\Documents\Test.docx";

Does this make any difference?

destinationPath=@"c:\program files (x86)\steam\steamapps\common\portal 2\Test\Test.docx";
FileLocation=@"C:\Users\Yoshie\Local Settings\Documents\Test.docx";
零時差 2024-12-06 09:20:51

你的目标文件路径应该是这样的

destinationPath="c:\\program files (x86)\\steam\\steamapps\\common\\portal 2\\Test\\Test.docx"

Your destination file path should be like this

destinationPath="c:\\program files (x86)\\steam\\steamapps\\common\\portal 2\\Test\\Test.docx"
埋葬我深情 2024-12-06 09:20:51

在执行 File.Delete 时,我也被这个 TargetInitationException 捕获,并且没有注意到确保目录存在的内部消息。

这是因为我从发布切换到调试,并且未能创建一组包含要删除的文件的相关子文件夹。

I was also caught out by this TargetInvocationException when doing File.Delete and did not notice the inner message of make sure the directory exists.

This was due to me switching from Release to Debug and I had failed to create a set of relative subfolders that would of contained the file to be deleted.

吐个泡泡 2024-12-06 09:20:51

就我而言(WinForm .NET Framework 4.7.2),使用路径长于 MAX_PATH(大约 260 个字符)的 File.Move 似乎也会触发此异常。

因此,在传递给 File.Move 之前,我在使用的路径前面添加了长路径语法,并且它起作用了。

// Prepend long file path support
if( !packageFile.StartsWith( @"\\?\" ) )
    packageFile = @"\\?\" + packageFile;

看:
如何处理具有名称的文件长度超过 259 个字符?

In my case (WinForm .NET Framework 4.7.2), using the File.Move with a path longer than MAX_PATH (around 260 characters) seems to also trigger this exception.

So I prepend the path I used with long path syntax before passing to File.Move and it worked.

// Prepend long file path support
if( !packageFile.StartsWith( @"\\?\" ) )
    packageFile = @"\\?\" + packageFile;

See:
How to deal with files with a name longer than 259 characters?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文