Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.IO.DirectoryNotFoundException by File.Move

Tags:

c#

file-io

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!

like image 655
Joshua Walsh Avatar asked Aug 12 '11 10:08

Joshua Walsh


2 Answers

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

like image 113
Zenwalker Avatar answered Nov 17 '22 16:11

Zenwalker


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";
like image 27
Haris Hasan Avatar answered Nov 17 '22 17:11

Haris Hasan