文件.复制和 WPF

发布于 2024-07-24 05:52:03 字数 525 浏览 10 评论 0原文

我对 WPF 中的 File.Copy 方法有一个小问题,我的代码非常简单,当我运行它时出现异常,

Could not find a part of the path 'Images\37c31987-52ee-4804-8601-a7b9b4d439fd.png'.

其中 Images 是一个相对文件夹。

这是我的代码,正如我所说的简单,相同的代码在控制台应用程序中运行良好,完全没有问题。

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;

所以如果有的话可以帮忙,谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

乖乖兔^ω^ 2024-07-31 05:52:03

images 文件夹是否存在? File.Copy 不会自动创建它。

你知道你当前的目录是什么吗? 文件打开/保存框可以改变这一点。 因此使用绝对路径总是更安全。

执行 a

Path.GetFullPath(filename)

看看它指向哪里。 位置合适吗?

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?

鯉魚旗 2024-07-31 05:52:03

如果使用绝对路径而不是相对路径,那么它可以工作吗?

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

在访问文件之前,应该调用 System.IO.File.Exists()。 从您的错误描述中不清楚原始文件在复制之前是否存在。

如果您不指定绝对路径,则您的相对路径通常会从意外的位置解析,通常是进程的当前工作目录。 调用此方法可能会告诉您进程当前正在运行:

System.IO.Directory.GetCurrentDirectory()

您永远不应该假设正在运行的进程的当前工作目录,因为用户可以从任何地方启动您的程序。 即使您认为您始终控制当前工作目录,您也会惊讶地发现自己经常出错。

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.

醉南桥 2024-07-31 05:52:03

你有调试器吗? 为什么不插入断点并检查每一步使用的值?

如果文件系统说“找不到文件”,我不会费心与它争论......

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

慈悲佛祖 2024-07-31 05:52:03

如果文件路径目录位于本地,则使用 \\ 作为文件路径目录。如果您的文件存在于网络路径中,请使用 \\\\(首先)..以便它查找网络驱动器。

谢谢

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

寄意 2024-07-31 05:52:03

有必要将所有外部文件嵌入到可执行文件中,并更改代码以使用这些嵌入文件,而不是期望磁盘上的文件。

要使用图像或任何您需要的文件(“xml/txt/doc”),您需要将文件的构建操作设置为嵌入资源,并使用文件的完全限定名称调用该方法,其中名称是汇编的像这样:

[RootNameSpaceOfTheProject].[NameOfFolderInTheProject].[FileNameWithExtension]

示例:

在此处输入图像描述

调用方法:

var b = ResourceOperations.GetResourceAsByteArray("Store.Resources.EmbeddedIcons.toolbox.png");
现在,您可以将字节数组写入临时文件,并将其用作图像源,或者您可以直接从字节数组构建图像。 至少,您已经获得了数据......

并且要将这些文件保存到磁盘,我们应该通过 @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);
     }
  }

然后调用它:

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

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);
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文