c# - How to extract from exe file the same .ico file as the one used to create this exe? -


i'm trying make kind of sfx: make program generating wrapping.exe around wrapped.exe.

wrapping.exe embed wrapped.exe resource and, when executed, wrapped.exe saved temporary folder, executed specific command line arguments , deleted. wrapped.exe not .net program , don't have source code it.

wrapping.exe should done in .net 3.5 usable on windows 7 sp1 , upper without prior .net installation.

wrapping.exe generated c# program using roselyn in .net 4.6.

i need wrapping.exe visualized wrapped.exe explorer. i've made successful test hardcoded .ico file. code (simplified):

var compilation = csharpcompilation.create(...); var resourcedescription = new resourcedescription( resourcename: "sfx.resourcename",                                                    dataprovider: () => file.openread("wrapped.exe"),                                                    ispublic:     false);  using (var iconstream = file.openread(@"wrapped.ico")) using (var pestream = file.create("wrapping.exe")) using (var pdbstream = file.create("wrapping.pdb")) using (var win32resstream = compilation.createdefaultwin32resources(                                                               versionresource:  true,                                                               nomanifest:       false,                                                               manifestcontents: null,                                                               iconinicoformat:  iconstream)) {     var emitresult = compilation.emit( pestream:          pestream,                                        pdbstream:         pdbstream,                                        manifestresources: new[] { resourcedescription },                                        win32resources:    win32resstream,                                        options:           new emitoptions(subsystemversion: subsystemversion.windows7));   return emitresult; } 

now try iconstream "wrapped.exe". i've tried replace:

using (var iconstream = file.openread(@"wrapped.ico")) 

with:

var iconstream = new memorystream(); icon icon = icon.extractassociatedicon("wrapped.exe"); icon.save(iconstream); iconstream.seek(0, seekorigin.begin); 

but 32*32 icon.

how extract same .ico file (including formats, example 16*16 32 bits bmp, 32*32 32 bits bmp, 48*48 32 bits bmp, 64*64 32 bits bmp , 256*256 32 bits png) 1 used create 'wrapped.exe'?

this easy using iconlib. response in this question: thx @plutonix!

with following helper function (of course, extraction icon file name not hardcoded):

static stream geticonstream_extracticonusingiconlib(string filetoexecute) {     var multiicon = new multiicon();     multiicon.load(filetoexecute);      var extractedicofilename = @"c:\temp\icon.ico";     multiicon.save(extractedicofilename, multiiconformat.ico);      return file.openread(extractedicofilename); } 

we have replace:

file.openread(@"wrapped.ico") 

with

geticonstream_extracticonusingiconlib("wrapped.exe") 

this gives full solution:

var compilation = csharpcompilation.create(...); var resourcedescription = new resourcedescription( resourcename: "sfx.resourcename",                                                    dataprovider: () => file.openread("wrapped.exe"),                                                    ispublic:     false);  using (var iconstream = geticonstream_extracticonusingiconlib("wrapped.exe")) using (var pestream = file.create("wrapping.exe")) using (var pdbstream = file.create("wrapping.pdb")) using (var win32resstream = compilation.createdefaultwin32resources(                                                               versionresource:  true,                                                               nomanifest:       false,                                                               manifestcontents: null,                                                               iconinicoformat:  iconstream)) {     var emitresult = compilation.emit( pestream:          pestream,                                        pdbstream:         pdbstream,                                        manifestresources: new[] { resourcedescription },                                        win32resources:    win32resstream,                                        options:           new emitoptions(subsystemversion: subsystemversion.windows7));   return emitresult; } 

Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -