2016-05-06 18 views
2

我有一個簡單的WinForms應用程序,但它有一些嵌入式資源(在「資源」下的子文件夾中),我想將其複製到計算機上的文件夾中。目前,我有後者的工作(有明確的命名方法嵌入的資源,並在它應該去):通過嵌入式資源循環並複製到本地路徑

string path = @"C:\Users\derek.antrican\"; 

using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream("WINFORMSAPP.Resources.SUBFOLDER.FILE.txt")) 
using (Stream output = File.Create(path + "FILE.txt")) 
{ 
    input.CopyTo(output); 
} 

但我還在試圖找出如何讓前工作:通過循環所有「WINFORMSAPP.Resources.SUBFOLDER」文件夾中的資源並移動它們。我做了很多谷歌搜索,但我仍然不確定如何獲取這個子文件夾中每個嵌入式資源的列表。

任何幫助將非常感謝!

回答

4

開始通過獲取嵌入在程序集中的所有資源:

Assembly.GetExecutingAssembly().GetManifestResourceNames() 

可查看與您所需的子文件夾的名稱,這些名稱來看看他們是內部或外部它用一個簡單的調用StartsWith

通過名稱

現在循環,並獲得相應的資源流:

const string subfolder = "WINFORMSAPP.Resources.SUBFOLDER."; 
var assembly = Assembly.GetExecutingAssembly(); 
foreach (var name in assembly.GetManifestResourceNames()) { 
    // Skip names outside of your desired subfolder 
    if (!name.StartsWith(subfolder)) { 
     continue; 
    } 
    using (Stream input = assembly.GetManifestResourceStream(name)) 
    using (Stream output = File.Create(path + name.Substring(subfolder.Length))) { 
     input.CopyTo(output); 
    } 
}