Skip to content

NET 5.0 self-contained single-file winforms application crashes while accessing settings #44606

@t081as

Description

@t081as

Description

A .NET 5.0 self-contained application crashes while accessing application settings from a .settings file.

Reproduction

Create a new winforms project (net5.0-windows)

> dotnet new winforms

Open the project in Visual Studio and add a "Settings file" (e.g. "Settings1.settings")
Add the setting:

Name: Test
Type: string
Scope: User
Value: Hello

Update the Main method (Program.cs) to read the value:

using System;
using System.Windows.Forms;

namespace SettingsIssue
{
    static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string myValue = Settings1.Default.Test;

            Application.Run(new Form1());
        }
    }
}

Add single file publish options to the csproj file:

<!-- ... -->
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<!-- ... -->

Publish the application (self-contained) and run it.

> dotnet publish --self-contained

Expected behavior:
The form is shown.

Actual behavior:
No form is shown; the windows event log contains the following exception:

Application: SettingsIssue.exe
CoreCLR Version: 5.0.20.51904
.NET Version: 5.0.0
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileNotFoundException: Das System kann die angegebene Datei nicht finden. (0x80070002)
   at System.Reflection.RuntimeModule.GetFullyQualifiedName()
   at System.Reflection.RuntimeModule.get_Name()
   at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
   at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
   at System.Configuration.Internal.ConfigurationManagerInternal.System.Configuration.Internal.IConfigurationManagerInternal.get_ExeProductName()
   at System.Configuration.ApplicationSettingsBase.get_Initializer()
   at System.Configuration.ApplicationSettingsBase.CreateSetting(PropertyInfo propertyInfo)
   at System.Configuration.ApplicationSettingsBase.EnsureInitialized()
   at System.Configuration.ApplicationSettingsBase.get_Properties()
   at System.Configuration.SettingsBase.GetPropertyValueByName(String propertyName)
   at System.Configuration.SettingsBase.get_Item(String propertyName)
   at System.Configuration.ApplicationSettingsBase.GetPropertyValue(String propertyName)
   at System.Configuration.ApplicationSettingsBase.get_Item(String propertyName)
   at SettingsIssue.Settings1.get_Test()
   at SettingsIssue.Program.Main()

Configuration

.NET SDK (gemäß "global.json"):
Version: 5.0.100
Commit: 5044b93829

Laufzeitumgebung:
OS Name: Windows
OS Version: 10.0.19041
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.100\

Host (useful for support):
Version: 5.0.0
Commit: cf258a1

.NET SDKs installed:
5.0.100 [C:\Program Files\dotnet\sdk]

Metadata

Metadata

Assignees

No one assigned

    Labels

    untriagedNew issue has not been triaged by the area owner

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions