2011-03-12 22 views
18

Tengo un ícono en mi archivo de recursos, al que quiero hacer referencia.Ruta a un archivo de recursos incrustado

Este es el código que necesita esa ruta a un archivo de icono:

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" 

En lugar de tener un archivo de icono externa quiero que encontrar un archivo de icono incrustado. Algo como

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

¿Esto es posible? si es así, cómo ?

Gracias

Respuesta

4

Creo que le ayudará en lo que algunos ...

//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

Eso no funcionará como IWshShortcut.IconLocation es una cadena, y Image.FromStream() es una imagen. Tienes que escribir la Imagen en un archivo, y apuntar el IconLocation a eso. – imoatama

5

creo que esto debería funcionar, pero no puedo recordar exactamente (no en el trabajo a doble control).

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

El recurso está incrustado, por lo que se incapsula en un conjunto de DLL. Entonces no puedes encontrar su camino real, tienes que cambiar tu enfoque.

Probablemente quiera cargar el recurso en la memoria y anotarlo en un archivo temporal, luego vincularlo desde allí. Una vez que se cambia el ícono en el archivo de destino, puede eliminar el archivo de ícono.

1

En WPF he hecho esto antes:

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

Sólo ampliando SharpUrBrain's answer, que no funcionaba para mí, en lugar de:

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

Debería ser algo como:

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