在.exe

时间:2015-08-18 14:29:32

标签: c# exe embedded-resource

我使用Costura.Fody将每个dll编译成我的.exe

我有一个.ico,我的项目用它来创建程序的快捷方式,以便在启动时启动程序,或者删除它(用户可以通过复选框添加/删除它)

但是我希望能够将.ico作为.exe的一部分包含在内,并能够引用它而不是使用路径(在.exe目录中使用.ico。这可能吗?作为资源?

目前我在做:

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);
        }
    }

1 个答案:

答案 0 :(得分: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);
            }
        }