Steve
Steve

Reputation: 31

Embedding a .ico within a .exe

I use Costura.Fody to compile every dll into my .exe

I have a .ico that my project uses to create a shortcut for the program for starting the program on startup, or removing it ( The user can add/remove it via a checkbox )

However I want to be able to include this .ico as part of the .exe and be able to reference it instead of using a path ( Currenntly using .ico in directory of the .exe. Is this possible? As a resource?

Currently I am doing:

public void CreateShortcut(string shortcutName, string shortcutPath, string targetFileLocation)
    {
        string shortcutLocation = System.IO.Path.Combine(shortcutPath, shortcutName + ".lnk");
        WshShell shell = new WshShell();
        IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);

        shortcut.Description = "Description";   // The description of the shortcut
        shortcut.IconLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "icon.ico");
        shortcut.TargetPath = targetFileLocation;                 // The path of the file that will launch when the shortcut is run
        shortcut.Save();                                    // Save the shortcut
    }

    public void DeleteShortcut()
    {
        // should find the file and delete it
        string[] dirs = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "StartupApp*");
        foreach (string dir in dirs)
        {
            System.IO.File.Delete(dir);
        }
    }

Upvotes: 2

Views: 839

Answers (1)

Steve
Steve

Reputation: 31

public void CreateShortcut(string shortcutName, string shortcutPath, string targetFileLocation)
        {
            string shortcutLocation = System.IO.Path.Combine(shortcutPath, shortcutName + ".lnk");
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation);

            shortcut.Description = "Description";   // The description of the shortcut
            //shortcut.IconLocation = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "icon.ico");
            Icon icon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);
            string pathToSave = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"StartupApp", "icon.ico");
            FileStream stream = new System.IO.FileStream(pathToSave, System.IO.FileMode.Create);
            icon.Save(stream);
            stream.Close();


            shortcut.IconLocation = pathToSave;
            shortcut.TargetPath = targetFileLocation;                 // The path of the file that will launch when the shortcut is run
            shortcut.Save();                                    // Save the shortcut
        }

        public void DeleteShortcut()
        {
            // should find the file and delete it
            string[] dirs = Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "StartupApp*");
            foreach (string dir in dirs)
            {
                System.IO.File.Delete(dir);
            }
        }

Upvotes: 1

Related Questions