Winform Application Configuration on Install
This question is related to Winform application deployment and configuration.
I am new to .net development and I am working on my first Winforms application. Whenever I deploy the application I need to set the value of the server to which the user will be connecting.
For this I have to go into program files and change the application configuration file manually using Notepad.
Not only have I heard that this is not a good practice, it is also a tedious exercise. Imagine going to half a dozen users and doing the same thing on each machine.
I am very confused as to what to do. The application config file that is in the same folder as the exe is the file thorugh which I can change the connection string (as I am given to understand).
Can anyone help me to solve the following questions.
How do I update the application configuration file after I install it without having to open it up in Notepad? My main concern is upd开发者_JAVA技巧ating the connection string.
I have heard that using the registry is a possibility in this case. I am scared of using the registry! Is this an option?
Please excuse my ignorance if the questions are naive. I am new to .net and looking for answers
Thanks Romi
Hi Romi here is a code snippet that can help you in read/right application configurations
for write in the
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration
(ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Add("ModificationDate",
DateTime.Now.ToLongTimeString() + " ");
// Save the changes in App.config file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
to read
foreach(string key in ConfigurationManager.AppSettings)
{
string value = ConfigurationManager.AppSettings[key];
Console.WriteLine("Key: {0}, Value: {1}", key, value);
}
my advice: if your are saving several settings that are not connection strings use the above code. just create a simple form, create a dictionary fill it and show it using a grid control.
if you only need to read/write connection strings use the following code
to read
string cnx = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
to write
ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString=" your conenction string here";
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("ConnectionString");
you can still use the registry to save those settings but I belive the code above will solve your problems.
精彩评论