我问,因为我已经到处搜索而没有找到结果......
我已经尝试过这两种方法:
//first attempt
Var filePath = System.Reflection.Assembly.GetExecutingAssembly (). Location + @"\..\..\Resources\Image.jpg";
//Second attempt
System.Reflection.Assembly.GetExecutingAssembly (). GetManifestResourceNames ();
已经探究了他们没有给出结果的这些方法我只需要获得我项目图像的路径。 求助。
答案 0 :(得分:0)
确保项目的Build Action设置为Embedded Resource。
您可能会发现这两种方法很有用,传入资源的name
和assemblies
列表 - 它会按顺序搜索程序集,从而提供简单的覆盖资源功能
private static Tuple<Assembly, string> FindEmbeddedResource(string name, params Assembly[] assemblies)
{
foreach (var assembly in assemblies)
{
var resourceName = assembly.GetManifestResourceNames().FirstOrDefault(r => r.EndsWith(name));
if (resourceName != null)
{
return new Tuple<Assembly, string>(assembly, resourceName);
}
}
return new Tuple<Assembly, string>(null, null);
}
/// <summary>
/// Gets the embedded resource scanning assemblies in the order supplied. Allows APIs to override library assets.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="assemblies">The assemblies.</param>
/// <returns></returns>
private static string GetEmbeddedResource(string name, params Assembly[] assemblies)
{
var embeddedResource = FindEmbeddedResource(name, assemblies);
var assembly = embeddedResource.Item1;
var resource = embeddedResource.Item2;
if (assembly != null)
{
var stream = assembly.GetManifestResourceStream(resource);
using (var textStreamReader = new StreamReader(stream))
{
resource = textStreamReader.ReadToEnd();
}
}
return resource;
}