如何从资源中获取文件流?(.net)

3
我在资源文件夹(resources)中有一些xsd文件,用于验证接收到的xml消息。我使用的资源文件名为AppResources.resx,其中包含一个名为clientModels.xsd的文件。当我尝试像这样使用文件:AppResources.clientModels时,我得到了一个包含文件内容的字符串。我希望获得一个流。 我不想使用assembly.GetManifestResourceStream,因为我曾经因此遇到过问题(使用这些流来归档SharpZipLib文件时出现问题)。 还有其他方法吗?我听说过ResourceManager - 它能帮助我吗?
3个回答

3

你可以将获取到的字符串输入到 System.IO.StringReader 中,这样可能会实现你想要的功能。你也可以查看 MemoryStream。


1
我有一个作为资源加载的zip文件,直接从命名空间引用它会给我字节而不是字符串。在资源设计器中右键单击文件,并将文件类型从文本更改为二进制。然后你会得到一个字节数组,你可以将它加载到MemoryStream中。

1

这里是链接中的代码

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

这段代码不是我写的,它来自于别处。

http://www.dreamincode.net/code/snippet1683.htm

HTH

骨架


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接