2017-08-10 84 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; 

最后一个是最容易与智能感知。