2017-08-10 87 views
2

我想從源文件中提取嵌入資源。從編譯源提取資源

爲此,我使用編譯:

CompilerParameters CP = new CompilerParameters(); 
[...code...] 

CP.EmbeddedResources.Add(@"C:\WindowsFormsApp3.exe"); 

My ressource + the source.txt

現在,我有在的ressource我的文件:with the ressource

但我怎麼能提取物 - 呢?我用這個話題:How to Read an embedded resource as array of bytes without writing it to disk? 但它是行不通的,沒有出現在我的文件夾中:/

所以,如果你能幫助我,我要提取的文件夾中的ressource。

謝謝你。

+0

好吧,我要去測試它。 –

+0

嘿,你能給我一個截圖請,我有一個命名空間的問題:/謝謝你:) –

回答

0

當您使用Resources.resx(默認情況下)將現有文件添加到項目中時,生成操作默認爲無。您需要選擇資源文件夾下的文件,並將構建操作指向Embeded Resource,這意味着它將包含在程序集或Content中,這意味着它不會包含在組件中。

要獲得所有內嵌資源的列表,你可以使用這個:

string[] embededResources = Assembly.GetExecutingAssembly().GetManifestResourceNames(); 

要使用資源:

//OctoBox: Picturebox 
//Octocat: Octocat.png image 

//Tested with: 
//Build action: Embeded Resource 
// 1st: 
OctoBox.Image = (Bitmap)Resources.ResourceManager.GetObject("Octocat"); 

//2nd 
Assembly asm = Assembly.GetExecutingAssembly(); 
Stream octoStream = asm.GetManifestResourceStream($"{asm.GetName().Name}.Resources.Octocat.png"); 

if (octoStream != null) 
{ 
    OctoBox.Image = Image.FromStream(octoStream); 
    octoStream.Close(); 
} 

//Tested with: 
//Build action: None, Content and embeded resource 
OctoBox.Image = Resources.Octocat; 

最後一個是最容易與智能感知。