C# 如何发现嵌入资源的“路径”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I discover the "path" of an embedded resource?
提问by Rob
I am storing a PNG as an embedded resource in an assembly. From within the same assembly I have some code like this:
我将 PNG 作为嵌入资源存储在程序集中。在同一个程序集中,我有一些这样的代码:
Bitmap image = new Bitmap(typeof(MyClass), "Resources.file.png");
The file, named "file.png" is stored in the "Resources" folder (within Visual Studio), and is marked as an embedded resource.
名为“file.png”的文件存储在“Resources”文件夹(在 Visual Studio 中)中,并被标记为嵌入资源。
The code fails with an exception saying:
代码失败,出现异常:
Resource MyNamespace.Resources.file.png cannot be found in class MyNamespace.MyClass
在类 MyNamespace.MyClass 中找不到资源 MyNamespace.Resources.file.png
I have identical code (in a different assembly, loading a different resource) which works. So I know the technique is sound. My problem is I end up spending a lot of time trying to figure out what the correct path is. If I could simply query (eg. in the debugger) the assembly to find the correct path, that would save me a load of headaches.
我有相同的代码(在不同的程序集中,加载不同的资源)可以工作。所以我知道这项技术是合理的。我的问题是我最终花了很多时间试图找出正确的路径。如果我可以简单地查询(例如在调试器中)程序集以找到正确的路径,那将为我省去很多麻烦。
采纳答案by John
This will get you a string array of all the resources:
这将为您提供所有资源的字符串数组:
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
回答by Konrad Rudolph
I'm guessing that your class is in a different namespace. The canonical way to solve this would be to use the resources class and a strongly typed resource:
我猜你的班级在不同的命名空间中。解决这个问题的规范方法是使用资源类和强类型资源:
ProjectNamespace.Properties.Resources.file
Use the IDE's resource manager to add resources.
使用 IDE 的资源管理器添加资源。
回答by Dylan
I find myself forgetting how to do this every time as well so I just wrap the two one-liners that I need in a little class:
我发现自己也每次都忘记了如何执行此操作,所以我只是将我需要的两个单行包在一个小班级中:
public class Utility
{
/// <summary>
/// Takes the full name of a resource and loads it in to a stream.
/// </summary>
/// <param name="resourceName">Assuming an embedded resource is a file
/// called info.png and is located in a folder called Resources, it
/// will be compiled in to the assembly with this fully qualified
/// name: Full.Assembly.Name.Resources.info.png. That is the string
/// that you should pass to this method.</param>
/// <returns></returns>
public static Stream GetEmbeddedResourceStream(string resourceName)
{
return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
}
/// <summary>
/// Get the list of all emdedded resources in the assembly.
/// </summary>
/// <returns>An array of fully qualified resource names</returns>
public static string[] GetEmbeddedResourceNames()
{
return Assembly.GetExecutingAssembly().GetManifestResourceNames();
}
}
回答by user3356450
The name of the resource is the name space plus the "pseudo" name space of the path to the file. The "pseudo" name space is made by the sub folder structure using \ (backslashes) instead of . (dots).
资源的名称是名称空间加上文件路径的“伪”名称空间。“伪”名称空间由子文件夹结构使用 \(反斜杠)而不是 . (点)。
public static Stream GetResourceFileStream(String nameSpace, String filePath)
{
String pseduoName = filePath.Replace('\', '.');
Assembly assembly = Assembly.GetExecutingAssembly();
return assembly.GetManifestResourceStream(nameSpace + "." + pseduoName);
}
The following call:
以下调用:
GetResourceFileStream("my.namespace", "resources\xml\my.xml")
will return the stream of my.xml located in the folder-structure resources\xml in the name space: my.namespace.
将返回位于命名空间中文件夹结构 resources\xml 中的 my.xml 流:my.namespace。
回答by masterwok
I use the following method to grab embedded resources:
我使用以下方法来抓取嵌入式资源:
protected static Stream GetResourceStream(string resourcePath)
{
Assembly assembly = Assembly.GetExecutingAssembly();
List<string> resourceNames = new List<string>(assembly.GetManifestResourceNames());
resourcePath = resourcePath.Replace(@"/", ".");
resourcePath = resourceNames.FirstOrDefault(r => r.Contains(resourcePath));
if (resourcePath == null)
throw new FileNotFoundException("Resource not found");
return assembly.GetManifestResourceStream(resourcePath);
}
I then call this with the path in the project:
然后我用项目中的路径调用它:
GetResourceStream(@"DirectoryPathInLibrary/Filename")