8

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!

pravprab
  • 2,301
  • 3
  • 26
  • 43
Joshua Walsh
  • 1,915
  • 5
  • 25
  • 50

5 Answers5

3

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

Zenwalker
  • 1,883
  • 1
  • 14
  • 27
  • Do I put the @ in front of the original path or the destination? – Joshua Walsh Aug 12 '11 at 10:04
  • Can I do File.Move(@FileLocation, @destinationPath); instead? – Joshua Walsh Aug 12 '11 at 10:07
  • Dude nope, it should be used only if you wish to escape the literal meaning of \ char i.e in used usually in paths. – Zenwalker Aug 12 '11 at 10:09
  • Yeah, just figured that out and then refreshed the page. Thanks, but it doesn't seem to work still... – Joshua Walsh Aug 12 '11 at 10:10
  • Well with updated code try and let us know. May be edit your post. – Zenwalker Aug 12 '11 at 10:11
  • 1
    -1: `/` is just as valid as `\\`. No need to dictate personal preferences :) – leppie Aug 12 '11 at 10:19
  • Yeah, just did a test and you can mix and match them as much as you want! YAY! – Joshua Walsh Aug 12 '11 at 10:23
  • @Leppie, Its just for redability sake. A good programmer writes code for humans and not for machines to understand. Plus in the OPs case, first let him correct the redability aspect and then test it and then come back with errors by making sure the source and destination paths are perfectly valid. Sorry mate, gotta vote down your comment. Not good that you encourage this. – Zenwalker Aug 12 '11 at 10:23
  • +1 as you are correct to encourage readability in code. No end of mistakes occur because of messy code. – iCollect.it Ltd Aug 12 '11 at 10:28
  • 1
    Funny, I primarily care about the compiler understanding my code. I actually find `/` more readable, but as I said, that is just a personal opinion. – leppie Aug 12 '11 at 10:29
2

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";
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
0

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?

Wappenull
  • 1,181
  • 13
  • 19
0

Your destination file path should be like this

destinationPath="c:\\program files (x86)\\steam\\steamapps\\common\\portal 2\\Test\\Test.docx"
Rasel
  • 15,499
  • 6
  • 40
  • 50
0

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.

WhoIsRich
  • 4,053
  • 1
  • 33
  • 19