C# 如何从资源中获取文件作为流?(。网)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/384789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get file from resources as a stream? (.net)
提问by agnieszka
I've got few files in resources (xsd files) that i use for validating received xml messages. The resource file i use is named AppResources.resxand it contains a file called clientModels.xsd. When i try to use the file like this: AppResources.clientModels, i get a string with the file's content. i would like to get a stream instead. i do not wish to use assembly.GetManifestResourceStream as i had bad experiences with it (using these streams to archive files with SharpZipLib didn't work for some reason). is there any other way to do it? i've heard about ResourceManager - is it anything that could help me?
我在用于验证收到的 xml 消息的资源(xsd 文件)中有几个文件。我使用的资源文件名为AppResources.resx,它包含一个名为clientModels.xsd的文件。当我尝试使用这样的文件时:AppResources.clientModels,我得到一个包含文件内容的字符串。我想要一个流。我不希望使用 assembly.GetManifestResourceStream,因为我对它有不好的体验(使用这些流来存档文件与 SharpZipLib 因某种原因不起作用)。有没有其他方法可以做到?我听说过 ResourceManager - 有什么可以帮助我的吗?
采纳答案by J Cooper
Could you feed the string you get into a System.IO.StringReader, perhaps? That may do what you want. You may also want to check out MemoryStream.
你能把你得到的字符串输入到 System.IO.StringReader 中吗?这可能会做你想做的。您可能还想查看 MemoryStream。
回答by dbones
here is the code from the link
这是链接中的代码
//Namespace reference
using System;
using System.Resources;
#region ReadResourceFile
/// <summary>
/// method for reading a value from a resource file
/// (.resx file)
/// </summary>
/// <param name="file">file to read from</param>
/// <param name="key">key to get the value for</param>
/// <returns>a string value</returns>
public string ReadResourceValue(string file, string key)
{
//value for our return value
string resourceValue = string.Empty;
try
{
// specify your resource file name
string resourceFile = file;
// get the path of your file
string filePath = System.AppDomain.CurrentDomain.BaseDirectory.ToString();
// create a resource manager for reading from
//the resx file
ResourceManager resourceManager = ResourceManager.CreateFileBasedResourceManager(resourceFile, filePath, null);
// retrieve the value of the specified key
resourceValue = resourceManager.GetString(key);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
resourceValue = string.Empty;
}
return resourceValue;
}
#endregion
I did not write the code it came from
我没有写它来自的代码
http://www.dreamincode.net/code/snippet1683.htm
http://www.dreamincode.net/code/snippet1683.htm
HTH
HTH
bones
骨头
回答by devlord
I have a zip file loaded as a resource, and referencing it directly from the namespace gives me bytes, not a string. Right-click on your file in the resources designer, and change the filetype from text to binary. Then you will get a bytearray, which you could load into a MemoryStream.
我有一个 zip 文件作为资源加载,直接从命名空间引用它会给我字节,而不是字符串。在资源设计器中右键单击您的文件,并将文件类型从文本更改为二进制。然后您将获得一个字节数组,您可以将其加载到 MemoryStream 中。