如何将文件从资源复制到另一个目录 [英] How to copy files from resources to another directory

查看:95
本文介绍了如何将文件从资源复制到另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

我试图将存储为wpf项目中资源的文件复制到新目录中.

这是要创建一个文件夹模板集,并在用户选择的目录中填充默认文件.我已经设法通过一些更简单的文件类型(例如图标和纯文本)来做到这一点,但是我正在努力寻找一种方法来复制其他文件.

除了流读取器和写入器之外,还有没有最好的方法来复制它们?这似乎是交出一些文件的空文件?

Hey,

I am trying to copy files stored as resources in my wpf project into a new directory.

This is to create a template set of folders with the default files populating it in the directory of the users choice. I have managed to do this with some of the easier file types like icons and plain text but am struggling with finding a way to copy others.

Is there a best way to make a copy of them other than stream reader and writer? this seemes to be handing back empty files for some files?

System.Drawing.Icon icon = Properties.Resources.icons;
              Stream IconStream = System.IO.File.OpenWrite(path+"\\Icons\\test.ico");
              icon.Save(IconStream);


这对于一个图标来说效果很好,但是以下代码对我的一个帮助文件不起作用;它创建了它,但是当我查看文件时,它似乎是空的并且不会打开


This works fine for an icon but the below code does not work for one of my help files; it creates it but when i look at the file it seems to be empty and wont open

  writer = new StreamWriter(path + "\\Help\\en\\test.chm");
writer.Write(Properties.Resources.test);


(resources.test是.chm文件)

谢谢!


(resources.test being a .chm file)

thanks!

推荐答案

认为我的最后一个答案有点误解.这是我实际测试过的代码,应该可以正常工作.

think there was a little misunderstanding in my last answer. here is code i actually tested and which should work.

string[] fileNames = Assembly.GetExecutingAssembly().GetManifestResourceNames();
foreach (String fileName in fileNames)
{
    using (FileStream fileStream = File.Create(@"c:\temp\" + fileName))
    {
Assembly.GetExecutingAssembly().GetManifestResourceStream(fileName).CopyTo(fileStream);
    }
}



请注意,资源必须标记为嵌入式资源"才能正常工作(在解决方案资源管理器中右键单击它们,构建操作嵌入式资源").
抱歉,也许不是100%解决了.



note that the resource must be marked as "embedded resources" for this to work (right click them in solution explorer, build action "embedded resources").
Maybe not 100% solved, im sorry.


这篇关于如何将文件从资源复制到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆