简体   繁体   中英

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.

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?

如果您使用绝对路径而不是相对路径,那么它行得通吗?

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.

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:

在此处输入图片说明

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);
   }

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM