2

How do I create a simple f# netcoreapp using .NET Core 3.0 or 3.1. and have an application icon show up when viewed in Windows explorer?

This is like a question posed before but the solutions doesn't seem to be working for an application built using .NET Core 3.0 or 3.1 sdks.

I have created a resource file and attempt to compile the application and add the icon resource using the the command line:

fsc Program.fs --win32res:AppIcon.res --target:exe

In this case the application compiles but I don't get a Windows explorer icon.

I've also tried to build and publish using the dotnet cli with the fsproj file below (dotnet publish ConsoleApp1.fsproj -c Release --self-contained -r win7-x64 -o publish)

In this case the project does not compile and I get an error "error MSB4018: The "CreateAppHost" task failed unexpectedly".

Any help would be greatly appreciated!

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <Win32Resource></Win32Resource>
  </PropertyGroup>
 
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <OtherFlags>--win32res:AppIcon.res</OtherFlags>
  </PropertyGroup>

  <ItemGroup>
    <EmbeddedResource Include="Resources\rebalance.ico" />
    <Resource Include="AppIcon.res">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Resource>
    <Compile Include="Program.fs" />
  </ItemGroup>
</Project>
SNC19030
  • 41
  • 3

3 Answers3

2

Looks like I was able to figure out the issue. I went back and looked at the resource file in Visual Studio and found out that there appeared to be and issue with the file?

enter image description here

I had used "C:\Program Files (x86)\Windows Kits\10\bin\10.0.18362.0\x64\rc.exe" /v AppIcon.rc to generate the res file. Maybe it got corrupted? I was still able to view the ico in Visual Studio even in this state. In any event, I deleted the corrupted ico file from the res and then added it back through Visual Studio manually. This time it appeared that Visual Studio recognized the file.

enter image description here

I used this res file to continue with my efforts. I re-ran the command line dotnet publish TestIconRes.fsproj -c Release --self-contained -r win7-x64 -o publish and this time the application icon was visible from Windows explorer.

fsc Program.fs --win32res:AppIcon.res also works.

The final fsproj file is below:

<Project Sdk="Microsoft.NET.Sdk">
  
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <Win32Resource></Win32Resource>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <OtherFlags>--win32res:AppIcon.res</OtherFlags>
  </PropertyGroup>

  <ItemGroup>
    <Compile Include="Program.fs" />
  </ItemGroup>
</Project>
SNC19030
  • 41
  • 3
1

Try this (with the .ico file in your project folder):

  <PropertyGroup>
    <ApplicationIcon>rebalance.ico</ApplicationIcon>
  </PropertyGroup>
  <ItemGroup>
    <Content Include="rebalance.ico">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
Scott Hutchinson
  • 1,703
  • 12
  • 22
  • Thanks very much for your suggestion Scott. I wasn't able to get this version of the project file to add an icon that shows up in Windows explorer. It looks like there was an undiscovered issue at the time I posted my question that was causing the problem (see my answer below) – SNC19030 Oct 29 '19 at 04:26
0

I discovered this issue as well and filed an issue in .NET Core for the PropertyGroup as Scott Hutchinson suggested as the solution.

https://github.com/dotnet/core/issues/3771

nickybaby
  • 1
  • 2