473,399 Members | 4,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Writing out to a .config file.

I know in .Net 1.1 you can't write out to an app.config file but I thought
you can in 2.0.

Can somebody please post code that will show how to write to the config
file?

TIA - Jeff.
May 2 '07 #1
2 1647
"Mufasa" <jb@nowhere.comwrote in message
news:uP**************@TK2MSFTNGP03.phx.gbl...
Can somebody please post code that will show how to write to the config
file?
I use the following, specifically for the appSettings section:
using System.Configuration;

/// <summary>
/// Adds / updates appSettings with the specified key/value pair dictionary
/// </summary>
/// <param name="pdicSettings">Dictionary of key/value pairs</param>
/// <returns>Boolean</returns>
public static bool SaveSettings(Dictionary<string, stringpdicSettings)
{
Configuration objConfigFile =
ConfigurationManager.OpenExeConfiguration(Configur ationUserLevel.None);

bool blnKeyExists;

foreach (KeyValuePair<string, stringkvpSetting in pdicSettings)
{
blnKeyExists = false;
foreach (string strKey in
objConfigFile.AppSettings.Settings.AllKeys)
{
if (strKey == kvpSetting.Key) // key already exists
{
blnKeyExists = true;
objConfigFile.AppSettings.Settings[kvpSetting.Key].Value =
kvpSetting.Value;
break;
}
}
if (!blnKeyExists) // key doesn't already exist, so add it
{
objConfigFile.AppSettings.Settings.Add(kvpSetting. Key,
kvpSetting.Value);
}
}

objConfigFile.Save(ConfigurationSaveMode.Modified) ;
ConfigurationManager.RefreshSection("appSettings") ;
return true;
}
--
http://www.markrae.net

May 2 '07 #2
Thanks.

J.

"Mark Rae" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
"Mufasa" <jb@nowhere.comwrote in message
news:uP**************@TK2MSFTNGP03.phx.gbl...
>Can somebody please post code that will show how to write to the config
file?

I use the following, specifically for the appSettings section:
using System.Configuration;

/// <summary>
/// Adds / updates appSettings with the specified key/value pair
dictionary
/// </summary>
/// <param name="pdicSettings">Dictionary of key/value pairs</param>
/// <returns>Boolean</returns>
public static bool SaveSettings(Dictionary<string, stringpdicSettings)
{
Configuration objConfigFile =
ConfigurationManager.OpenExeConfiguration(Configur ationUserLevel.None);

bool blnKeyExists;

foreach (KeyValuePair<string, stringkvpSetting in pdicSettings)
{
blnKeyExists = false;
foreach (string strKey in
objConfigFile.AppSettings.Settings.AllKeys)
{
if (strKey == kvpSetting.Key) // key already exists
{
blnKeyExists = true;
objConfigFile.AppSettings.Settings[kvpSetting.Key].Value =
kvpSetting.Value;
break;
}
}
if (!blnKeyExists) // key doesn't already exist, so add it
{
objConfigFile.AppSettings.Settings.Add(kvpSetting. Key,
kvpSetting.Value);
}
}

objConfigFile.Save(ConfigurationSaveMode.Modified) ;
ConfigurationManager.RefreshSection("appSettings") ;
return true;
}
--
http://www.markrae.net

May 3 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: msnews.microsoft.com | last post by:
Hi all, I was trying to find an easy way to write in the standard configuration (like app.config) xml-based files. I found classes that can be used to read this information (in...
4
by: UJ | last post by:
Is there a way to write into the .config file? In other words treat it like a .ini file. (I may be confused on what the exact nature of the .config file may be.) I can read from it fine but...
2
by: MK | last post by:
Is there any way to write into the app config at runtime? I want to store position, height and width of my forms.
19
by: Noozer | last post by:
I need to keep my application settings in a file that users can copy/backup/etc. Before I start using the old INI file standard, is there any easy way to use XML files to hold application...
1
by: ROO | last post by:
Hi Everyone, I need some help Creating Database Connection at runtime and writing Connection String to App.Config, I have 3mxtboxes on a form txtUsername = SQL admin User txtPassword = Sql admin...
9
by: WT | last post by:
Hello, I am writing a Web install project and I want to get the connection string from user and write it in the web.config of the installed web site. As the Websetup is a windows program, I use...
0
by: Rum | last post by:
Hi, I`m writing an application that can be updated from remote server. Application config file is updating too (adding new sections etc...) When application is updating i make a copy of old config...
6
by: Peted | last post by:
Hi, im wanting to store some custom text strings in the app.config file of a c# app, to be retreived and updated when the app runs. using c# 2005 express in my testing i am using the code...
0
parshupooja
by: parshupooja | last post by:
Hey All, I have old vb.net application and I have various users for that.where I have XML file as datasource. Till now for each user application was installed seperately. only difference...
3
by: John | last post by:
Hi Is there any easy way to write strings into a file for debugging purposes? Thanks Regards
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.