2011-03-12 213 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; 
}