pavelmed
asked on
C# - How to create, read from and write to a custom configuration file?
I have a C# console app that I have to modify.
The app uses a config file to save to and read from some values that could be used for future runs. It does not use properties file for that because with the properties file you still can write to but that properties file would be different for each user, and this should be user independent.
That;s why the config file is used.
Currently, the app has an app.config file that resides in the main directory where all the source code is located, and <app_name>.config file that is located in the executable file's directory (\bin\Debug in this case). The values stored by the application in the <app_name>.config file that is located in the executable file's directory.
But every time the application is recompiled, the <app_name>.config file is being overwritten by the app.config file located in source directory, and the custom values are reset to the values stored in the app.config file.
Questions: 1) is it possible to change valued in the app.config file as well as in the <app_name>.config file (by the application) so that the new values are not reset during compilation.
2) If not, what are other options? My idea is to use a custom config file that would be could be used to write to and read from custom data just as the <app_name>.config file is.
But how and where to create, read from and write to a custom configuration file?
Code samples would be much appreciated.
The current code to read from and to write to looks like this:
Configuration config = ConfigurationManager.OpenE xeConfigur ation(Conf igurationU serLevel.N one);
string storedValue = config.AppSettings.Setting s["Param1" ].Value
string newValue = "param1_newValue";
config.AppSettings.Setting s["Param1" ].Value = newValue;
config.Save(ConfigurationS aveMode.Mo dified);
ConfigurationManager.Refre shSection( "appSettin gs");
Thank you!
The app uses a config file to save to and read from some values that could be used for future runs. It does not use properties file for that because with the properties file you still can write to but that properties file would be different for each user, and this should be user independent.
That;s why the config file is used.
Currently, the app has an app.config file that resides in the main directory where all the source code is located, and <app_name>.config file that is located in the executable file's directory (\bin\Debug in this case). The values stored by the application in the <app_name>.config file that is located in the executable file's directory.
But every time the application is recompiled, the <app_name>.config file is being overwritten by the app.config file located in source directory, and the custom values are reset to the values stored in the app.config file.
Questions: 1) is it possible to change valued in the app.config file as well as in the <app_name>.config file (by the application) so that the new values are not reset during compilation.
2) If not, what are other options? My idea is to use a custom config file that would be could be used to write to and read from custom data just as the <app_name>.config file is.
But how and where to create, read from and write to a custom configuration file?
Code samples would be much appreciated.
The current code to read from and to write to looks like this:
Configuration config = ConfigurationManager.OpenE
string storedValue = config.AppSettings.Setting
string newValue = "param1_newValue";
config.AppSettings.Setting
config.Save(ConfigurationS
ConfigurationManager.Refre
Thank you!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sorry I had to delay my final comments due to unrelated issues I was fully occupied with.
I ended up with a compromised solution - I kept saving the changes in the <app_name>.config file in the executable file directory, but after that I also saved the new values in the app.config file in the source code directory so that if a project is recompiled, the <app_name>.config and app.config files would have the same values.
// keeping the newly saved values in the <app_name>.config file
config.AppSettings.Setting s["Param1" ].Value = newValue;
config.Save(ConfigurationS aveMode.Mo dified);
ConfigurationManager.Refre shSection( "appSettin gs");
// saving the new values also in the app.config file in the source code directory (if the directory and file exists)
// In my project, the exe file directory is two levels below the source code directory.
// and of course, I surrounded the code in try/catch block (now shown below)
string appExePath = Path.GetDirectoryName(Appl ication.Ex ecutablePa th);
int lastBSPos = appExePath.LastIndexOf("\\ ");
string appConfigPathDir = appExePath.Substring(0, lastBSPos);
lastBSPos = appConfigPathDir.LastIndex Of("\\");
appConfigPathDir = appConfigPathDir.Substring (0, lastBSPos + 1);
if (Directory.Exists(appConfi gPathDir))
{
config.SaveAs(appConfigPat hDir + "app.config", ConfigurationSaveMode.Full );
AppDomain.CurrentDomain.Se tData("APP _CONFIG_FI LE", appConfigPathDir + "app.config");
}
I am going to split the points between participants of this call.
Thank you!
I ended up with a compromised solution - I kept saving the changes in the <app_name>.config file in the executable file directory, but after that I also saved the new values in the app.config file in the source code directory so that if a project is recompiled, the <app_name>.config and app.config files would have the same values.
// keeping the newly saved values in the <app_name>.config file
config.AppSettings.Setting
config.Save(ConfigurationS
ConfigurationManager.Refre
// saving the new values also in the app.config file in the source code directory (if the directory and file exists)
// In my project, the exe file directory is two levels below the source code directory.
// and of course, I surrounded the code in try/catch block (now shown below)
string appExePath = Path.GetDirectoryName(Appl
int lastBSPos = appExePath.LastIndexOf("\\
string appConfigPathDir = appExePath.Substring(0, lastBSPos);
lastBSPos = appConfigPathDir.LastIndex
appConfigPathDir = appConfigPathDir.Substring
if (Directory.Exists(appConfi
{
config.SaveAs(appConfigPat
AppDomain.CurrentDomain.Se
}
I am going to split the points between participants of this call.
Thank you!
ASKER
I ended up with a compromised solution
ASKER
I have a console application with no web access.
Can the example still be used for the ConfigurationManager?