2011-03-12 214 views
18

我在我的資源文件中有一個圖標,我想引用它。嵌入式資源文件的路徑

這是需要一個圖標文件路徑的代碼:

IWshRuntimeLibrary.IWshShortcut MyShortcut ; 
MyShortcut = (IWshRuntimeLibrary.IWshShortcut)WshShell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\PerfectUpload.lnk"); 
MyShortcut.IconLocation = //path to icons path . Works if set to @"c:/icon.ico" 

,而不必我希望它找到一個嵌入的圖標文件外部圖標文件。 類似於

MyShortcut.IconLocation = Path.GetFullPath(global::perfectupload.Properties.Resources.finish_perfect1.ToString()) ; 

這是可能的嗎?如果是的話如何?

感謝

回答

4

我認爲這將幫助你在一些什麼......

//Get the assembly. 
System.Reflection.Assembly CurrAssembly = System.Reflection.Assembly.LoadFrom(System.Windows.Forms.Application.ExecutablePath); 

//Gets the image from Images Folder. 
System.IO.Stream stream = CurrAssembly.GetManifestResourceStream("ImageURL"); 

if (null != stream) 
{ 
    //Fetch image from stream. 
    MyShortcut.IconLocation = System.Drawing.Image.FromStream(stream); 
} 
+5

這將不起作用,因爲IWshShortcut.IconLocation是一個字符串,Image.FromStream()是一個Image。您必須將圖像寫入文件,並在該位置指向IconLocation。 – imoatama 2012-09-27 01:45:16

5

我認爲這應該工作,但我不記得確切(而不是在工作,仔細檢查)。

MyShortcut.IconLocation = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNamespace.IconFilename.ico"); 
1

它被嵌入的資源,所以封裝在一個DLL程序集中。所以你不能得到真正的道路,你必須改變你的方法。

您可能需要將資源加載到內存中,並將其寫入臨時文件,然後從那裏鏈接它。一旦圖標在目標文件上被更改,您可以刪除圖標文件本身。

1

在WPF我以前做過這樣的:

Uri TweetyUri = new Uri(@"/Resources/MyIco.ico", UriKind.Relative); 
System.IO.Stream IconStream = Application.GetResourceStream(TweetyUri).Stream; 
NotifyIcon.Icon = new System.Drawing.Icon(IconStream); 
4

只擴大SharpUrBrain's answer,這對我不起作用,而不是:

if (null != stream) 
{ 
    //Fetch image from stream. 
    MyShortcut.IconLocation = System.Drawing.Image.FromStream(stream); 
} 

它應該是這樣的:

if (null != stream) 
{ 
    string temp = Path.GetTempFileName(); 
    System.Drawing.Image.FromStream(stream).Save(temp); 
    shortcut.IconLocation = temp; 
}