简体   繁体   中英

Path to an embedded resource file

I have an icon in my resource file , which I want to reference.

This is the code that needs that path to an icon file:

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" 

Instead of having an external icon file I want it to find an embedded icon file. Something like

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

is this possible ? if so how ?

Thanks

Just expanding on SharpUrBrain's answer , which didn't work for me, instead of:

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

It should be something like:

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

我认为这应该有用,但我记不清楚了(不是在工作中仔细检查)。

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

I think it will help you in some what...

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

The resource it is embedded, so incapsulated in a DLL assembly. So you cannot get its real path, you have to change your approach.

You would probably want to load the resource in memory and write it down to a temp file, then link it from there. Once the icon is is changed on the destination file, you can delete the icon file itself.

In WPF I have done this before:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM