Create app config file if doesn't exist C#
Asked Answered
O

6

9

When I try to open my app config file by

ConfigurationManager.OpenExeConfiguration(filePath);

it returns an exception, because there is no file. But I need to create this file in this case. But as I know, config file create by adding at VS like How to: Add an Application Configuration File to a C# Project

So, how to create this file in other way?

Obovoid answered 16/12, 2013 at 15:12 Comment(4)
Why would you crate config file if user deleted it? Check if file exists before opening it (or handle exception) and use default values if config not foundFrustration
I think, user doesn't delete it. File doesn't exist at the beginning.Obovoid
It's just a text file - you can create it just like you would any other text file. But the proper way is to include it in your project at design time.Deck
I think the actual problem is that filePath should the path of the exe, not of the config file. Then you don't get an exception and when you call Save() the file will be created for you.Ilse
S
16

If you really want to create an empty config file at runtime you can do something like this, but note that if the config file already exists this code will overwrite it:

System.Text.StringBuilder sb = new StringBuilder();
sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
sb.AppendLine("<configuration>");
sb.AppendLine("</configuration>");

string loc = Assembly.GetEntryAssembly().Location;
System.IO.File.WriteAllText(String.Concat(loc, ".config"), sb.ToString());
Slouch answered 16/12, 2013 at 15:21 Comment(4)
"sb.toString()" should be "sb.ToString()", otherwise perfect answer. Thanks!Theroid
Great catch @RobVermeulen, thanks! I've edited the answer.Slouch
This is a half answer.Chantellechanter
You could check if the file is already there, that would make it more complete.Theroid
F
1

Just go to App.config file properties and change Copy to Output Directory setting from Do not copy to Copy always (or Copy if newer). That will automatically create config file during project build.

Frustration answered 16/12, 2013 at 15:18 Comment(3)
How to create myapp.exe.config if there is no App.config in project?Obovoid
@ArtemKyba why not simply add App.config to project?Frustration
Client wants without adding:)Obovoid
M
1

This file is needed by your application as you need to access it. The best way is to add it with Visual Studio as you wrote it. You have to ensure that it is properly deployed on the target machine at setup.

Then if it is not present on disk at runtime, it's a good behaviour to crash. You can display a message to the user (catch the exception).

Mallette answered 16/12, 2013 at 15:18 Comment(0)
P
1

Why would you want to create it any other way? Is it to make sure you always have a config file, even if the user deletes it? And why would a user do that? Oh never mind.

If you want to make sure there's always a config file, you can create it yourself if it isn't there:

if (!File.Exists(configFilePath)) ...

Config files are just XML. So you could create a new file and populate it with default data, right before you want to read a key from it.

However one would expect a user not to delete a config file. Just sayin'.

Profitsharing answered 16/12, 2013 at 15:19 Comment(1)
This kind of solution always smells like race condition; .NET Framework ignores TxF.Chantellechanter
I
1

For those who are looking the answer in 2022, app.config will be created automatically if you will change the .NET Target Framework in Project properties

Inviolate answered 7/4, 2022 at 18:3 Comment(0)
A
-1

To add an application configuration file to a C# project

On the menu bar, choose Project > Add New Item.*

The Add New Item dialog box appears.

Expand Installed > Visual C# Items, and then choose the Application Configuration File template.

In the Name text box, enter a name, and then choose the Add button. A file named app.config is added to your project.

Annabelannabela answered 20/10, 2021 at 14:27 Comment(2)
I'm not sure we need you to cut and paste the Ms docs into an answer. A simple comment to the link would of sufficedElfreda
It also totally misses the point of the question...Elfreda

© 2022 - 2024 — McMap. All rights reserved.