My.Settings() Throws Exception Error When Using .NET Core, But Not When Using .NET Framework

1.4k views Asked by At

When creating a project with any version .NET Core, debugging the application throws this error:

System.Configuration.ConfigurationErrorsException: 'Configuration system failed to initialize'

Inner Exception ConfigurationErrorsException: Unrecognized configuration section system.diagnostics. (C:\Users[UserName]\Documents\Visual Studio 2022\Projects\WinFormsApp1\WinFormsApp1\bin\Debug\net6.0-windows\WinFormsApp1.dll.config line 8)

However, when creating a project with any version of .NET Framework, the application debugs and runs perfectly fine, with no errors or exceptions. Both projects were created as fresh clean projects. Both projects were created exactly the same, the only difference being .NET Core or .NET Framework.

I'm using Visual Studio 2022, VB.NET, and creating a Windows Forms App (VB.) The versions of .NET Core I have installed: [3.1], [5.0], and [6.0]. the versions of .NET Framework I have installed: [2.0], 3.0], [3.5], [4.6], [4.6.1], [4.6.2], [4.7], [4.7.1], [4.7.2], and [4.8].

The steps for both projects are as follows: Open Visual Studio 2022 -> Create A New Project -> Select Windows Forms App (.NET Core - any version) or (.NET Framework - any version) -> Project Name: WindowsApp1, Location: [My Location], Solution Name: WindowsApp1, Place solution and project in same directory: (I've tried checked and unchecked), Framework: (I've tried any and all versions of .NET Core and .NET Framework) -> Create

Add Checkbox to empty form -> Checkbox name is default (CheckBox1 - I've tried changing the name) -> Project Properties -> Settings -> Add Setting - Name: cbState (I've tried different names), Type: Boolean, Scope: User, Value: True or False (I've tried both)

The code for both projects is exactly the same:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckBox1.Checked = My.Settings.cbState
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        My.Settings.cbState = CheckBox1.Checked
        My.Settings.Save()
    End Sub
End Class

I try to debug the application -> The .NET Framework version runs with no exceptions, and works as intended - saving the checkbox state of CheckBox1 and restoring it when re-launching the application.

The .NET Core version throws the exception error as listed above.

Again, all steps were executed in the exact same way, through the exact same process.

The Settings.Designer.vb file is highlighted ///here when the exception is thrown:

        <Global.System.Configuration.UserScopedSettingAttribute(),  _
         Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
         Global.System.Configuration.DefaultSettingValueAttribute("False")>  _
        Public Property cbState() As Boolean
            Get
                Return CType(Me("cbState"),Boolean) ///This is the highlighted line
            End Get
            Set
                Me("cbState") = value
            End Set
        End Property
    End Class
End Namespace

I've also tried to Synchronize the Settings.

I have a project that I've spent hours on, that was created using .NET Core 6.0. When I encountered it, I decided to try and reproduce this error with a new project, with the result being the same exception being thrown with .NET Core. I then tried using .NET Framework instead of .NET Core, and the issue was resolved. I encountered this error when trying to use My.Settings() in the project. I've searched online for an answer, but could not find anything. If anyone has any solution or if I'm missing something, please let me know. I would like to find a solution that does not require me to create a new project from scratch using .NET Framework instead of .NET Core, as I'd have to start from scratch (I'd assume.) All I'm trying to do is bind property settings to an object (such as a checkbox.) In VB.NET 6.0 Core it seems Microsoft removed the easily accessible (ApplicationSettings) binding from the properties window, so setting a binding to property has to be done manually, from what I've heard. Any help would be appreciated!

1

There are 1 answers

0
Barfunkle On

Deleting the <system.diagnostics> section from the app.config file resolved the exception, and the application was then able to save and restore My.Settings() properly.