C# 嵌入资源文件的路径
我的资源文件中有一个图标,我想引用它 这是需要图标文件路径的代码:C# 嵌入资源文件的路径,c#,.net,C#,.net,我的资源文件中有一个图标,我想引用它 这是需要图标文件路径的代码: IWshRuntimeLibrary.IWshShortcut MyShortcut ; MyShortcut = (IWshRuntimeLibrary.IWshShortcut)WshShell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\PerfectUpload.lnk"); My
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()) ;
这可能吗?如果是,怎么做
谢谢我想这应该行得通,但我记不清了(不是在工作中反复检查)
它所嵌入的资源,因此被封装在DLL程序集中。所以你不能得到它的真正路径,你必须改变你的方法
您可能希望将资源加载到内存中,并将其写入临时文件,然后从那里链接它。一旦目标文件上的图标被更改,您就可以删除图标文件本身。res协议可以帮助您做到这一点:我认为它将在某些方面帮助您
//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);
}
在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);
只是扩展一下,这对我来说不起作用,而不是:
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;
}
这不起作用,因为IWshShortcut.IconLocation是字符串,Image.FromStream()是图像。您必须将图像写入一个文件,并指向该文件的IconLocation。谢谢,我一直在寻找一种标准化的方法,将程序集嵌入的资源表示为URI。我很高兴看到这一点,甚至来自MSDN。可能是重复的
if (null != stream)
{
string temp = Path.GetTempFileName();
System.Drawing.Image.FromStream(stream).Save(temp);
shortcut.IconLocation = temp;
}