Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path to an embedded resource file

Tags:

c#

.net

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

like image 327
gyaani_guy Avatar asked Mar 12 '11 15:03

gyaani_guy


People also ask

How do you read embedded files?

Method 1: Add existing file, set property to Embedded Resource. Add the file to your project, then set the type to Embedded Resource . NOTE: If you add the file using this method, you can use GetManifestResourceStream to access it (see answer from @dtb).

How do you add an embedded resource?

Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.

What is embedded resource C#?

Embedded files are called as Embedded Resources and these files can be accessed at runtime using the Assembly class of the System.Reflection namespace. This article will illustrate, how to read and display an embedded Text file and Image file in Windows Application (Windows Forms) in C# and VB.Net.


2 Answers

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");
like image 178
Jason Down Avatar answered Sep 26 '22 01:09

Jason Down


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;
}
like image 45
imoatama Avatar answered Sep 27 '22 01:09

imoatama