abdelkarim
abdelkarim

Reputation: 596

File.Copy and WPF

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.

Upvotes: 2

Views: 6423

Answers (6)

StepUp
StepUp

Reputation: 38209

It is necessary to embed all external files into the executable and change your code to work with these embedded files rather than to expect files on the disk.

To use images or whatever you need files("xml/txt/doc"), you need to set the build action of your file to Embedded Resource, and call the method with the fully qualified name of the file, where the name is assembled like this:

[RootNameSpaceOfTheProject].[NameOfFolderInTheProject].[FileNameWithExtension]

Example:

enter image description here

Call the method:

var b = ResourceOperations.GetResourceAsByteArray("Store.Resources.EmbeddedIcons.toolbox.png"); Now you can write the byte array to a temporary file for example and use this as an image source, or you can build an image from the byte array directly. At least, you've got your data...

and to save this files to a disk we should write a code by @Jon Skeet :

 public static void CopyStream(Stream input, Stream output)
 {
     // Insert null checking here for production
     byte[] buffer = new byte[8192];

     int bytesRead;
     while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
     {
         output.Write(buffer, 0, bytesRead);
     }
  }

then call it:

   using (Stream input = assembly.GetManifestResourceStream(resourceName))
   using (Stream output = File.Create(path))
   { 
      CopyStream(input, output);
   }

Upvotes: 0

csensoft
csensoft

Reputation: 743

use \\ for the file path directory if it in local.. if your file exists in network path use \\\\(atfirst).. So that it look for network drive..

Thanks

Upvotes: 0

Michael Maddox
Michael Maddox

Reputation: 12489

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.

Upvotes: 1

Benjol
Benjol

Reputation: 66637

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...

Upvotes: 0

user112889
user112889

Reputation: 805

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

Upvotes: 2

chris166
chris166

Reputation: 4807

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?

Upvotes: 2

Related Questions