从NET Core 3.0中的代码获取ApplicationIcon

时间:2019-02-28 08:55:11

标签: c# .net .net-core .net-core-3.0

在.csproj中,我为.NET Core 3.0应用程序分配了一个图标:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
    <ApplicationIcon>C:\temp\myicon.ico</ApplicationIcon>
  </PropertyGroup>

</Project>

图标将设置为生成的exe文件,并显示在任务管理器/文件浏览器中。

如何从代码中访问此图标?我不想从生成的exe中提取它或添加其他资源图标。

2 个答案:

答案 0 :(得分:2)

您可以这样提取

using System;
using System.Drawing;         //For Icon
using System.Reflection;      //For Assembly

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //Gets the icon associated with the currently executing assembly
                //(or pass a different file path and name for a different executable)
                Icon appIcon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);                
            }
            catch(ArgumentException ae) 
            {
                //handle
            }           
        }
    }
}

答案 1 :(得分:0)

//We can use this statement.

using System.Drawing;
using System.Reflection;

 static void Main(string[] args)
        {
            try
            {       
                Icon appIcon = Icon.ExtractAssociatedIcon(Assembly.GetExecutingAssembly().Location);                
            }
            catch(ArgumentException ae) 
            {
                //handle
            }