2008-12-21 67 views
3

我有几个文件在资源(xsd文件),我用于验证收到的XML消息。我使用的资源文件名为AppResources.resx,它包含一个名为clientModels.xsd的文件。当我尝试使用这样的文件:AppResources.clientModels时,我得到一个字符串与文件的内容。我想得到一个流代替。 我不想使用assembly.GetManifestResourceStream,因为我对它有不好的体验(使用这些流来归档SharpZipLib文件因为某种原因没有工作)。 有没有其他方法可以做到这一点?我听说过有关ResourceManager的信息 - 它可以帮助我吗?如何从资源中获取文件作为流? (.net)

回答

3

你可以将你得到的字符串送入System.IO.StringReader吗?这可能会做你想要的。你可能也想看看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

骨头

1

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