C# System.IO.DirectoryNotFoundException by File.Move

C# System.IO.DirectoryNotFoundException by File.Move,c#,file-io,C#,File Io,只是一个简短的问题(我希望): 当我使用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 maybe

只是一个简短的问题(我希望): 当我使用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"
谢谢! 编辑:我现在真的觉得自己像个白痴。我没有意识到目标文件夹必须存在!我愚蠢地认为,如果目标文件夹不存在,它将自动创建。很抱歉浪费了你的时间,不过还是要谢谢你的回答!(我现在知道我可以用@来阻止逃跑,所以知道这个很好)
无论如何,谢谢你,再一次,对不起

这有什么区别吗

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

请使用\而不是/以及使用@like@“path”。

您的目标文件路径应如下所示

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

在执行File.Delete时,我也发现了这个TargetInvocationException异常,但没有注意到内部消息“确保目录存在”


这是因为我从Release切换到Debug,并且我未能创建一组包含要删除的文件的相对子文件夹。

是否检查了destinationPath?它有正斜杠和反斜杠!再检查一下<代码>c:/program files(x86)/应该是c:\\program files(x86)\\@Anton,不是。至少以c#的方式。为你提供了一个明确的例子,并承认了你的错误。如果更多的程序员能够承认他们自己的错误,我们的行业将处于更好的状态:)我是将@放在原始路径还是目标路径的前面?我可以执行File.Move(@FileLocation,@destinationPath);相反?都德:不,只有当您想逃避\char的字面意义时才应该使用它,即通常在路径中使用的-1:
/
与“\\”一样有效。不需要规定个人偏好:)有趣的是,我主要关心编译器理解我的代码。事实上,我觉得
/
更具可读性,但正如我所说,这只是个人观点。-1:也因为不知道/和\是一回事,两者都是有效的。