17

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

gyaani_guy
  • 3,191
  • 8
  • 43
  • 51
  • Possible duplicate of [In C#, is there a way to get a string path reference to an embedded resource file?](https://stackoverflow.com/questions/11892908/in-c-is-there-a-way-to-get-a-string-path-reference-to-an-embedded-resource-fil) – StayOnTarget Nov 14 '19 at 14:24

6 Answers6

4

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;
}
Community
  • 1
  • 1
imoatama
  • 933
  • 6
  • 13
4

I think this should work, but I can't remember exactly (not at work to double check).

MyShortcut.IconLocation = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("YourNamespace.IconFilename.ico");
Jason Down
  • 21,731
  • 12
  • 83
  • 117
3

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);
}
SharpUrBrain
  • 3,180
  • 5
  • 38
  • 56
  • 6
    That won't work as IWshShortcut.IconLocation is a string, and Image.FromStream() is an Image. You have to write the Image out to a file, & point the IconLocation at that. – imoatama Sep 27 '12 at 01:45
2

The res protocol may be able to help you with this: http://msdn.microsoft.com/en-us/library/aa767740(v=vs.85).aspx

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • Thanks, I have been looking for a somewhat _standardized_ way to represent an assembly-embedded resources as a URI. I'm glad to see this, coming even from MSDN. – Ivaylo Slavov May 01 '14 at 08:59
1

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.

Luca Fagioli
  • 12,722
  • 5
  • 59
  • 57
1

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);
Peter C
  • 2,257
  • 2
  • 25
  • 28