tags:

views:

821

answers:

4

hi to everyone, i have a little problem with the File.Copy method in WPF, my code is very simple and i get an exception when i run it, << Could not find a part of the path 'Images\37c31987-52ee-4804-8601-a7b9b4d439fd.png'. >>, where Images is a relative folder.
here is my code, as i said simple and the same code works fine in a console application, no problem at all.

string filenamae = System.IO.Path.Combine(images, Guid.NewGuid().ToString() + System.IO.Path.GetExtension(imageFile)); ;
System.IO.File.Copy(imageFile, filenamae);
this.ImageLocation = string.Empty;

so if any can help, thanks.

+1  A: 

Does the images folder exist? File.Copy doesn't create it automatically.

Do you know what your current directory is? File open/save boxes can change that. So it's always safer to work with absolute paths.

Do a

Path.GetFullPath(filename)

and see where that points to. Is it the right location?

chris166
the folder existe.
abdelkrimnet
and even with your method, the exception is still there.
abdelkrimnet
+1  A: 

If you use the absolute instead of the relative path, does it work then?

the folder is in the same directory of the application, why need to be absolute, and besides why it works fine in a console application. that's what stopped me. why? What is the difference between a WPF App and a console App
abdelkrimnet
"why need to be absolute" -- Because your current directory will not always be the same as your EXE directory? The command prompt lets you start an EXE from a directory other than the current dir. Shortcuts let you specify the startup directory. In either case, do you really want your app to crash? Relative paths are almost always a bad idea.
Joe White
"why need to be absolute" - maybe you should try it and see if it works, then worry about why need to be absolute.
Josh Einstein
what if my absolute path doesn't exist, say i choose "d:\Images", but the 'd' drive doesn't exist, then i have to let the drive letter until tha app is deployed. don't want that.
abdelkrimnet
"what if my absolute path doesn't exist, say i choose "d:\Images", but the 'd' drive doesn't exist, then i have to let the drive letter until tha app is deployed. don't want that. – abdelkrimnet 7 hours ago" What do you mean by this? Your app will always need to check if the original file exists and if the location where you want to copy to exists.
A: 

Do you have a debugger? Why not insert a breakpoint and check the values used at each step?

If the file system says "cannot find file", I wouldn't bother arguing with it...

Benjol
both the values are correct. in fact, in a console app it works fine.
abdelkrimnet
When you say console application, do you mean 'not an winforms application' or do you mean 'in debug mode'? Whatever, try writing the values to the event log in the case where your code is NOT working...
Benjol
A: 

Before you access a file, you should call System.IO.File.Exists(). It's not clear from your error description if the origin file exists or not before the copy.

If you don't specify an absolute path, your relative path with often be resolved from unexpected places, usually the current working directory of the process. Calling this method may tell you were the process is currently running:

System.IO.Directory.GetCurrentDirectory()

You should never make assumptions about the current working directory of a running process as the user could start your program from anywhere. Even if you think you always control the current working directory, you will be surprised how often you will be wrong.

Michael Maddox