Why do some C# applications have an app.config file and others do not?

complete

Active member
Joined
Oct 24, 2012
Messages
25
Programming Experience
3-5
Why do some C# applications have an app.config file and others do not? Both of these solutions were initialized as Visual C# Windows Form Applications.
User's image



User's image
 
It just depends on the project template whether it's part of the template to create the file or not. Typically, later .NET Framework templates would automatically create it, but older .NET Framework would not. Based on that screenshots you have above, it looks like .NET Core has gone back to not creating it.
 
Based on that screenshots you have above, it looks like .NET Core has gone back to not creating it.

.NET Core expects to use appsettings.json.
 
Above, net 7 (core derived), Below net framework 4.8

1706178515213.png


(and guess what happens when you hit "Create or open", add a setting and build...)


1706178638340.png




Side note, WinForms in .net core derivatives can be a miserable experience; I'd still use netFW for WF unless there was some massively compelling reason to do otherwise
 
Thanks for pre-emptively answering my question if the VS IDE's Settings feature had been retrofitted to know about appsetting.json. Looks like it hasn't.
 
Seems like one option for sticking with app.config is to create a custom template with an app.config in the project and manually add in code to get settings.

Models:
public class PositionOptions
{
    public const string Position = "Position";

    public string Title { get; set; } = String.Empty;
    public string Name { get; set; } = String.Empty;
}

public class ConnectionStringOptions
{
    public const string Position = "ConnectionString";
    public string Production { get; set; } = String.Empty;
    public string Development { get; set; } = String.Empty;
}

Then

Configuration class:
public static class AppConfigService
{
    public static ConnectionStringOptions ConnectionStrings()
    {
        IConfigurationSection connectionsConfigurationSection = Configuration.Root().GetSection(ConnectionStringOptions.Position);
        return connectionsConfigurationSection.Get<ConnectionStringOptions>();
    }

    public static PositionOptions GeneralSettings()
    {
        IConfigurationSection positionConfigurationSection = Configuration.Root().GetSection(PositionOptions.Position);
        return positionConfigurationSection.Get<PositionOptions>();
    }

    public static string ApplicationKey()
    {
        return Configuration.Root()["AppKey"];
    }

}

public class Configuration
{
    public static IConfigurationRoot Root() =>
        new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddXmlFile("app.config", optional: true, reloadOnChange: true)
            .Build();
}

Then

app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <AppKey>XAS-1023</AppKey>
    <Position>
        <Title>Some title</Title>
        <Name>My name</Name>
    </Position>
    <ConnectionString>
        <Production>Data Source=.\SQLEXPRESS;Initial Catalog=NorthWind2024;Integrated Security=True;Encrypt=False</Production>
        <Development>Data Source=.\SQLEXPRESS;Initial Catalog=NorthWind2023;Integrated Security=True;Encrypt=False</Development>
    </ConnectionString>
</configuration>

Usage
Get settings:
ConnectionStringOptions connections = AppConfigService.ConnectionStrings();
PositionOptions generalSettings = AppConfigService.GeneralSettings();
var key = AppConfigService.ApplicationKey();
 
Back
Top Bottom