2

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);
        }
    }
Steve
  • 31
  • 3
  • Exe-icon should be a job of `Costura.Fody` making exe. And yes, you can add ico-file into resources: [wpf](http://stackoverflow.com/q/74466/1997232) / [winforms](http://stackoverflow.com/q/5656809/1997232). – Sinatr Aug 18 '15 at 14:40
  • Could I do something like: shortcut.IconLocation = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location); but by setting the shortcuts icon istead of setting its icon path? – Steve Aug 18 '15 at 15:03

1 Answers1

1
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);
            }
        }
Steve
  • 31
  • 3