2008-12-21 78 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中。