如何复制文件从资源? [英] How to copy file From Resources?
本文介绍了如何复制文件从资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个嵌入的资源文件,例如:的file.exe
如何在目录,例如复制: C:\
?在点击按钮
感谢
I have an embedded resources file eg: file.exe
how to copy in directory eg: c:\
?
at click button
thanks
推荐答案
您可以使用的 Assembly.GetManifestResourceStream
获得流读取你的资源。然后,只需将它复制到的FileStream
。如果您使用.NET 4中,你可以使用 Stream.CopyTo
做出这么简单:
You can use Assembly.GetManifestResourceStream
to get a stream to read your resource from. Then just copy it to a FileStream
. If you're using .NET 4, you could use Stream.CopyTo
to make that easy:
private void CopyResource(string resourceName, string file)
{
using (Stream resource = GetType().Assembly
.GetManifestResourceStream(resourceName))
{
if (resource == null)
{
throw new ArgumentException("No such resource", "resourceName");
}
using (Stream output = File.OpenWrite(file))
{
resource.CopyTo(output);
}
}
}
这篇关于如何复制文件从资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文