Search code examples
c#notepad++execs-script

CS-Script for notepad++ prepare exe for distribution with icon


I'm a complete newbie at C# I'm using Notepad++ for coding with CS-Script plugin for notepad++. I've developed a little script in it.

In such script I use a traybar icon that I made myself and I included a reference to it in the script with the following row of code

trayIcon.Icon = new Icon("icon.ico");

That works almost as I need... but look at the following image for clarification:

Binary with the embedded Icon file

Than I prepare the exe for distribution with the CS-Script command and it works beautifully. The problem is that the compiling procedure doesn't include the icon into the binary file so I must include by hands the icon file it self into the same directory of the binary file: in fact the exe file generated hangs with an error if icon file isn't included.

Question:

Is there a way to solve the problem and make the "binary package" all in one file with both the executable part and the ico resource file altogether?

Constraints:

I know I can create a rar, zip, or compressed exe (self-extracting file) but I don't mean that: I want 1 exe file with the icon in it that can be executed and moved stand alone. In other word the icon has to be embedded into the stand alone exe because it is the exe file icon itself and the icon for the windows traybar.

Screenshot of CS-Script in Notepad++


Solution

  • I have found this way to get Icon from the embedded resources in the CS-Script compiled binary. Icon file name is alpha.ico.

    Please pay attention to the using section.

    //css_resource alpha.ico
    
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using System.Reflection;
    
    class Script
    {
        [STAThread]
        static public void Main(string[] args)
        {
            var assembly = Assembly.GetExecutingAssembly();
    
            using (var stream = assembly.GetManifestResourceStream("alpha.ico"))
            {
                var icon = new Icon(stream);
            }
        }
    }