【问题标题】:Embedding a .ico within a .exe在 .exe 中嵌入 .ico
【发布时间】:2015-08-18 14:29:32
【问题描述】:

我使用 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);
        }
    }

【问题讨论】:

  • exe图标应该是Costura.Fody制作exe的工作。是的,您可以将 ico 文件添加到资源中:wpf / winforms
  • 我可以这样做:shortcut.IconLocation = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);但是通过设置快捷方式图标而不是设置其图标路径?

标签: c# exe embedded-resource


【解决方案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);
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2014-05-16
    相关资源
    最近更新 更多