【问题标题】:Path to an embedded resource file嵌入资源文件的路径
【发布时间】:2014-06-29 00:20:04
【问题描述】:

我的资源文件中有一个图标,我想引用它。

这是需要图标文件路径的代码:

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()) ;

这可能吗?如果有怎么办?

谢谢

【问题讨论】:

标签: c# .net


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    只是扩展 SharpUrBrain's answer,这对我不起作用,而不是:

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

    【讨论】:

      【解决方案3】:

      我认为它会对你有所帮助...

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

      【讨论】:

      • 这不起作用,因为 IWshShortcut.IconLocation 是一个字符串,而 Image.FromStream() 是一个图像。您必须将图像写入文件,并将 IconLocation 指向该文件。
      【解决方案4】:

      【讨论】:

      • 谢谢,我一直在寻找一种有点标准化的方法来将程序集嵌入的资源表示为 URI。我很高兴看到这一点,甚至来自 MSDN。
      【解决方案5】:

      在 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);
      

      【讨论】:

        【解决方案6】:

        它嵌入的资源,因此被封装在一个 DLL 程序集中。所以你无法得到它的真实路径,你必须改变你的方法。

        您可能希望将资源加载到内存中并将其写入临时文件,然后从那里链接它。在目标文件上更改图标后,您可以删除图标文件本身。

        【讨论】:

          猜你喜欢
          • 2011-02-02
          • 1970-01-01
          • 2010-09-06
          • 2019-01-02
          • 1970-01-01
          • 2017-07-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多