我有幾個文件在資源(xsd文件),我用於驗證收到的XML消息。我使用的資源文件名爲AppResources.resx,它包含一個名爲clientModels.xsd的文件。當我嘗試使用這樣的文件:AppResources.clientModels時,我得到一個字符串與文件的內容。我想得到一個流代替。 我不想使用assembly.GetManifestResourceStream,因爲我對它有不好的體驗(使用這些流來歸檔SharpZipLib文件因爲某種原因沒有工作)。 有沒有其他方法可以做到這一點?我聽說過有關ResourceManager的信息 - 它可以幫助我嗎?如何從資源中獲取文件作爲流? (.net)
3
A
回答
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中。
相關問題
- 1. 從資源文件/內容獲取流
- 2. 如何從資源中獲取文件
- 3. 如何從JAR文件獲取資源
- 4. 如何從spring獲取文件資源
- 5. 獲取從資源文件
- 6. .NET獲取嵌入的資源文件
- 7. 如何從AnyLogic 7的外部資源獲取資源流量?
- 8. 如何從資源中讀取圖像作爲流?
- 9. 如何從資源文件夾中獲取文件。 Spring框架
- 10. 如何從MSIL或.NET PE文件中提取資源內容
- 11. 如何獲取資源文件中所有資源的名稱
- 12. 添加ini文件作爲資源文件並從中讀取
- 13. 如何從相關工件中獲取資源文件
- 14. 從資源文件夾中獲取資源時出錯
- 15. 如何獲取測試資源文件?
- 16. 如何立即獲取文件資源?
- 17. 使用文件流從資源中讀取文件
- 18. 作爲資源讀取文本文件
- 19. 我如何從.dll文件獲取資源(圖片)?
- 20. 如何從.NET中的流獲取MemoryStream?
- 21. 從RESW資源獲取文件
- 22. 從Visual C++資源文件獲取FILEVERSION
- 23. 從JNLP文件獲取單個資源
- 24. 從Maven資源獲取文件路徑
- 25. 的Android:如何從資源文件中獲取圖像
- 26. 如何從資源文件中獲取圖像路徑
- 27. 如何從資源文件中獲取數組的數量?
- 28. 如何從用戶框架中獲取資源或nib文件?
- 29. 如何從java目錄中獲取資源包文件?
- 30. 如何從tomcat webapp中的context.xml文件獲取資源?