在C#.net下载文件从资源库 [英] Download Files From Resource Library in c# .net
问题描述
我发展的地方,从多个屏幕的用户必须下载一个示例文件(Excel文件)一起工作的应用程序。会有什么preferable的方式来做到这一点。
I am developing an application where from several screens user has to download a sample file(excel) to work with. What will be the preferable way to do so.
我在做什么是
配售文件在执行应用程序,并使用Application.StartupPath下载文件的目录。这不听起来像是一个非常好的解决方案。由于在任何时候用户可以编辑文件或删除文件或这样的事情。
Placing the file in the directory where application is executing and download the files using Application.StartupPath. This does not sounds like a very good solution. As at anytime user could edit the files or delete the files or such things.
我想要做的就是
我想从资源添加的所有文件在我的资源和下载文件。我想补充的文件,但我需要关于如何从资源下载的帮助不大。
I want to add all the files in my resources and download files from the resources. I add the files but i need a little help on how to download it from resources.
感谢
推荐答案
您可以使用:
class ResourceHelper
{
public static void MakeFileOutOfAStream(string stream, string filePath)
{
using(var fs = new FileStream(filePath,FileMode.Create,FileAccess.ReadWrite))
{
CopyStream(GetStream(stream), fs);
}
}
static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, read);
}
}
static Stream GetStream(string stream)
{
return Assembly.GetExecutingAssembly().GetManifestResourceStream(stream));
}
}
和与流过的你资源的完整名称,即命名空间+资源文件名(evantual夹将被认为是命名空间的一部分)的情况下敏感。请记住,标志项目文件作为嵌入资源
and with stream pass the complete name of you resource, that is the namespace + the resource file name ( evantual folder are to be considered as namespace part ) case sensitive. Remember to flag the file in your project as embedded resource.
这篇关于在C#.net下载文件从资源库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!