.NET command line compiler – embedding .ico files as resources

Clearly C# / .NET is missing all the purity of a proper programming language – when has any developer sat down with an editor and a terminal and made something with it?

Well, I am currently trying (it’s an open source app and I want it to build from a batch file without needing a proprietory IDE) and it’s hard.

The particular thing that irritated me today was embedding an ICO file into an assembly as a resource. I tried plain old
csc /resource:subdir/file.ico
I tried
resgen subdir/file.ico subdir/file.ico.resource

BUT NO, you can NOT has cheeseburger (said the compiler to the lolcat). I finally solved the issue by realising the following:

  • you need to embed the item at compile time
  • you need to extract it at runtime using an identifier
  • you need to specify the identifier when you embed at compile time.

Great Scott!! so, in build.bat:

csc [snip] /res:subdir/file.ico,app.file.ico [snip]

This embeds the resource with identifier "app.file.ico". In your application when you want to create an Icon instance from the file:

Icon i = new Icon(this.GetType().Assembly

There didn’t seem to be a clear answer on the subject on Google, so here you go internets! 🙂

(by the way this is using .NET 2.0, the standard SDK.)


About tehwalrus

Lib Dem, Software Developer, General Geek. View all posts by tehwalrus

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: