2015-08-18 19 views
1

我用Costura.Fody每一個DLL編譯成.exe文件我一個嵌入.ICO中的.exe文件

我有我的項目使用,爲在啓動時啓動該程序爲程序創建一個快捷方式的.ICO ,或刪除它(用戶可以通過複選框添加/刪除它)

但是我希望能夠包括這個.exe作爲.exe的一部分,並能夠引用它而不是使用路徑(當然使用.ico目錄下的.exe。這是可能嗎?作爲資源?

目前我在做:

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

Exe-icon應該是'Costura.Fody'製作exe的工作。是的,您可以將ico文件添加到資源中:[wpf](http://stackoverflow.com/q/74466/1997232)/ [winforms](http://stackoverflow.com/q/5656809/1997232)。 – Sinatr

+0

我可以做些什麼:shortcut.IconLocation = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly()。Location);但通過設置快捷方式圖標是爲了設置其圖標路徑? – Steve

回答

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