我有几个文件在资源(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. .NET获取嵌入的资源文件
- 3. 如何从资源中读取图像作为流?
- 4. 如何获取资源文件中所有资源的名称
- 5. 添加ini文件作为资源文件并从中读取
- 6. 如何从相关工件中获取资源文件
- 7. 从资源文件夹中获取资源时出错
- 8. 如何获取测试资源文件?
- 9. 如何立即获取文件资源?
- 10. 作为资源读取文本文件
- 11. 我如何从.dll文件获取资源(图片)?
- 12. 从JNLP文件获取单个资源
- 13. 从RESW资源获取文件
- 14. 从Visual C++资源文件获取FILEVERSION
- 15. 如何从.NET中的流获取MemoryStream?
- 16. 的Android:如何从资源文件中获取图像
- 17. 如何从jar中获取资源(文件夹)?
- 18. 如何从tomcat webapp中的context.xml文件获取资源?
- 19. 如何从资源文件中获取图像路径
- 20. 如何从用户框架中获取资源或nib文件?
- 21. 如何从资源中获取视图?
- 22. 获取资源文本文件列表
- 23. 强制设备从资源文件夹获取资源
- 24. 如何从dll文件获取字符串资源?
- 25. 如何从资源文件
- 26. 如何从xml文件中的资源xml文件夹中获取inputStream
- 27. 从资源中读取文本文件
- 28. 将.NET资源提取到文件
- 29. 从C#中的资源文件获取文件字节数
- 30. 从我的jar文件中获取资源文件夹内容?