question

LeeTaylor-7568 avatar image
0 Votes"
LeeTaylor-7568 asked cooldadtx commented

Where are the application settings ?

I have developed a new Winforms Application n .Net Core 6 in C#. I used the application settings to create an application setting for my database connection string. Once deployed the user would edit this information to point to their database. However I cannot find where the settings file was deployed (I did a folder deployment). Where can I find this file to edit it once I have installed my app on a different machine?

dotnet-runtime
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered cooldadtx commented

For .NET Core+ apps the settings file is called appsettings.json. It will be sitting in the same directory as the executable. If you don't have an appsettings.json in your project then create one and add the dummy connection string. Within the JSON file is the ConnectionStrings element where the connection string resides. Note however that this should be read only to the end user after installation so unless you update it during installation then it is not where you'd want to store settings changeable for the user.

.NET Core doesn't actually have any infrastructure in place for storing per-user data like .NET Framwork did. The general recommendation is to use a standard persistent storage like a custom data file in the user's document directory, perhaps the registry if you're desperate or even a database if the data is large.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The application will throw a friendly error if the connection string is not present, but the appsettings file is not there. It should be present in the bin folder for debug and/or release compiles, and it is not. It's not also not in the publication, but yet the app runs on a different machine which means the settings are present in some file. the only 2 JSON files are a DEP file and a runtime config file. Neither of which contain the Application Connection String.

Also User settings are being saved as well somewhere.

0 Votes 0 ·

.NET Core, out the box pulls its settings from the environment variables, command line arguments and configuration file as documented here. If your app is finding a connection string then it is either getting it from one of these or via some default your app is using. Without seeing that relevant code we have no way of knowing where.

Irrelevant, do you have an appsettings.json file in your project? If not then create one. Look at the properties for the file and ensure the Copy to Output is set to copy to the output directory. It should if you created the file using the project item UI. Now when you build the file will be in the output directory.

{
    "ConnectionStrings": {
        "MyApp": "My connection string"
    }
}


To access the connection string your code is ultimately going to do something like Configuration.GetConnectionString("MyApp") which will fetch the connection string, if any. But all this requires the hosting to be set up and it isn't (for whatever reason) in the default Winforms project. So follow the steps here.

As for publishing you'll need to use dotnet publishing otherwise it won't get your settings properly.

0 Votes 0 ·