2008-08-26 49 views
81

我將PNG作爲嵌入式資源存儲在程序集中。從同一個組件內我有一些像這樣的代碼:如何發現嵌入資源的「路徑」?

Bitmap image = new Bitmap(typeof(MyClass), "Resources.file.png"); 

文件,命名爲「file.png」存儲在「資源」文件夾(Visual Studio中),並標記爲嵌入的資源。

代碼失敗,異常話說:

資源MyNamespace.Resources.file.png不能在類MyNamespace.MyClass找到

我有相同的代碼(在不同的組件,加載一個不同的資源)哪個工作。所以我知道這項技術是健全的。我的問題是我花了很多時間去弄清楚正確的道路是什麼。如果我可以簡單地查詢(例如在調試器中)程序集以找到正確的路徑,那將爲我節省一大筆頭痛。

回答

156

這將讓你所有的資源的字符串數組:

System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames(); 
15

我猜你的班級在不同的命名空間。解決此問題的標準方法是使用資源類和強類型資源:

ProjectNamespace.Properties.Resources.file 

使用IDE的資源管理器添加資源。

+0

你是對的,我的課是在一個不同的命名空間。看起來Resources文件夾位於項目配置中指定爲默認命名空間的命名空間下,由於各種原因,該文件夾不屬於該類所屬的命名空間。 我懷疑你完全正確地使用了不同的方法,但由於我需要與遺留代碼保持一致,這超出了我的控制範圍。 – Rob 2012-04-14 22:52:14

+0

這會抓取資源 - 不是文件路徑。 – 2017-12-04 18:25:55

+0

@UchihaItachi這就是爲什麼我明確提出這個答案作爲解決*潛在問題*的另一個(而且可以說是規範的)方式,而不是逐字回答問題(無論如何都已經有答案)。 – 2017-12-04 18:47:45

41

我發現自己忘了如何做這所有的時間,以及所以我只換兩個單行,我需要在一個小類別:

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(); 
    } 
} 
3

資源的名稱是名稱空間加上文件路徑的「僞」名稱空間。 「僞」名稱空間由子文件夾結構使用\(反斜槓)代替。 (點)。

public static Stream GetResourceFileStream(String nameSpace, String filePath) 
{ 
    String pseduoName = filePath.Replace('\\', '.'); 
    Assembly assembly = Assembly.GetExecutingAssembly(); 
    return assembly.GetManifestResourceStream(nameSpace + "." + pseduoName); 
} 

以下電話:

GetResourceFileStream("my.namespace", "resources\\xml\\my.xml") 

將返回所在的文件夾結構資源my.xml流\ XML名稱空間中:my.namespace。

5

我用下面的方法搶嵌入的資源:

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); 
    } 

然後,我在項目的路徑稱之爲:

GetResourceStream(@"DirectoryPathInLibrary/Filename")