I know from the title this sounds like a duplicate. In truth, it's an oversimplification of the problem which seems to have many other symptoms that I confess I'm going to have difficulty articulating. I really didn't want to post this question, but I've searched it to death and hit a wall.
It's been some time since I last created a new WinForms app, and I noticed a number of things are... off. First, when creating the project I'm only given 2 options of target framework despite having multiple verions of .NET Framework installed.
I opted to go with the default option (.NET Core 3.1). This is a simple app that's meant to update the status of an item in the database. As with all of my projects, I like to keep the connection strings stored in app.config, however there was no such file generated with the new project. This is what it looks like in an older project:
I decided to forcibly add an app.config file (from the 'Add New Item' wizard in the project menu), only for the debugger to stop in the middle of the execution with only this error in the console:
exception of type 'System.TypeInitializationException' occurred...
After googling that problem, I learned that a conflict of config files can cause that error to be throw so I promptly deleted the one I created and decided to look into the debug directory.
This is where I noticed the directory structure is different than it used to be. Where previously the debug folder would contain the project files, now I see a folder netcoreapp3.1. No matter, upon exploring its contents I found a config file but with one difference. Previously config files were named like projectname.exe.config, where as this one is projectname.dll.config. None the less, I opened it up and added my Appsettings, but it does look different to what I'm used to:
At this point, the program runs without the previous exception being thrown, but now my connection string isn't being set because it won't pull the value from Appsettings. This is the line which should read the string:
Public Shared Cstring As String = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
Is there some other method I should be using to read the Appsettings keys, or maybe I need to make a different kind of config file?
CodePudding user response:
I don't know anything about the differing app.config formats, but I can tell you how to address the issue with only having .NET 5.0 or .NET Core as the project target.
If you look at the list of project templates available when you create a new project, the first one you see will be "Windows Forms App". As you have noted, this will only support .NET 5.0 or .NET Core. If you continue down the list, you will see "Windows Forms App (.Net Framework)". This is the project template to use to support a pre-5.0 / non Core .NET e.g. .NET Framework 4.8.