Could somebody help in writing a routine to Add/Edit/Delete a config File in 2.0
Lets suppose I have the following Config File
< xml version="1.0" encoding="utf-8" >
<configuration>
<connectionStrings>
<add name="Connection1" connectionString="Data Source=MYSERVER;Initial Catalog=Pubs;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="Connection2" connectionString="Data Source=MYSERVER;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
What I want to do is programmatically Add A new ConnectionString "Connection3" like
<add name="Connection3" connectionString="Data Source=MYSERVER;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient" />
any suggestions
Thanks

Get Connection String from app.config file to a class library.
YaliWei
>> When I read I always get a ConnectionString that I HAVE NOT ADDED.
Check out your machine.config file. http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconmachineconfigurationfiles.asp. Your application will get a "merged" view of what's in machine.config and in app.config.
>> "WinConfig.vshost.exe" HAS NEW CONN STRING
Your code adds the connection string to the currently running application's configuration file. If you run your app in VS under the debugger, and have the hosting process enabled, the application that actually get run is the <appname>.vshost.exe, and it's corresponding configuration file will be <appname>.vshost.exe - exactly what you are observing.
>> "WinConfig..exe" IS EMPTY
That's because your connection string ended up in WinConfig.vshost.exe.config
>> in my application . App.Config IS EMPTY
I assume that by "in my application" you really mean "in my project" If so, this is also expected. When Visual Studio builds your project, it will copy and rename this file to the appropriate output directories, and your application will read/write from/to those copies.
Best regards,
Johan Stenberg
AlexisP
Hai,
I just create a class library that will create db and tables, stored procedure and all. In data access layer i need to get the connection string from a app.config file in to that class library.
Can any one help me to do this.
Thanks,
With Cheers,
Matthewsnape
Thanks for your reply.
I have looked at the links and were very useful but someway I am not there yet. I have noticed the following.
When I read I always get a ConnectionString that I HAVE NOT ADDED.
"Name: LocalSqlServer Connection String : data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true Provider Name : System.Data.SqlClient"
When I save and then read again i get the connectionstring I have just added however if i shut down the app and look in the App.config it's not there.What's happening
When I am writing at runTime and look at my App.Configs.
Bin Directory
"WinConfig.vshost.exe" HAS NEW CONN STRING
"WinConfig..exe" IS EMPTY
in my application . App.Config IS EMPTY
Which one I should write I have also used the path but does not work.
Could you or anybody look at this code and see what I am doing wrong
I have added 2 buttons(Read and Save) and a combo to a form
=============
public void AddConnectionStrings(string path){
// Get the count of the connection strings. int connStrCnt = ConfigurationManager.ConnectionStrings.Count; string tmpConnName = "Name: " + "Conn1"; string tmpConnString = "Connection String : " + "data source=127.0.0.1;Integrated Security=SSPI;" + "Initial Catalog=aspnetdb"; string tmpProviderName = "Provider Name : " + "System.Data.SqlClient"; // Get the configuration file.System.Configuration.
Configuration config = ConfigurationManager.OpenExeConfiguration(path); // Add the connection string. ConnectionStringsSection csSection = config.ConnectionStrings;csSection.ConnectionStrings.Add(
new ConnectionStringSettings(tmpConnName, tmpConnString, tmpProviderName)); // Save the configuration file.config.Save(
ConfigurationSaveMode.Modified); Console.WriteLine("Connection string added.");}
// Add a connection string to the connection
// strings section and store it in the
// configuration file.
public void AddConnectionStrings(){
// Get the count of the connection strings. int connStrCnt = ConfigurationManager.ConnectionStrings.Count; string tmpConnName = "Name: " + "Conn1"; string tmpConnString = "Connection String : " + "data source=127.0.0.1;Integrated Security=SSPI;" + "Initial Catalog=aspnetdb"; string tmpProviderName = "Provider Name : " + "System.Data.SqlClient"; // Get the configuration file.System.Configuration.
Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Add the connection string. ConnectionStringsSection csSection = config.ConnectionStrings;csSection.ConnectionStrings.Add(
new ConnectionStringSettings(tmpConnName,tmpConnString,tmpProviderName)); // Save the configuration file.config.Save(
ConfigurationSaveMode.Modified); Console.WriteLine("Connection string added.");}
public void GetConnectionStrings(){
// string path=@"C:\Documents and Settings\Gabriel\My Documents\Visual Studio 2005\Projects\WindowsApplication2\WindowsApplication2\App.Config"; //System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(path);System.Configuration.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); this.comboBox1.Items.Clear(); foreach (ConnectionStringSettings connection in config.ConnectionStrings.ConnectionStrings){
string tmpConnName = "Name: " + connection.Name; string tmpConnString = "Connection String : " + connection.ConnectionString; string tmpProviderName = "Provider Name : " + connection.ProviderName; this.comboBox1.Items.Add(tmpConnName + " " + tmpConnString + " " + tmpProviderName);}
}
private void btnRead_Click(object sender, EventArgs e){
GetConnectionStrings();
}
private void btnSave_Click(object sender, EventArgs e){
//AddConnectionStrings(); //this does not work whyAddConnectionStrings(
@"D:\Programming\Development Library\System\Configuration\WinConfig\WinConfig\App.Config");}
==============
Thanks a lot for any replies in advance.
johncharle
To programmatically edit the config file, you need to manually open and edit it.
This is easier since the name is known (appDir/appName.exe.config), and the format is known. Open the file, edit, and save.
KSH
Thanks for your lengthy reply.It clarified few things!!
I made it work and I can apply Add/Read New ConnectionStrings but only to the application Executable path.
However is there any reason why it should not work by passing a path to any app.config file At the moment it only works with passing the ApplicationExecutable.Why
I am trying to write a small connectionString Editor to make it easy to add ConnectionString and i would like to point to any config file and add /edit.
My question is
Can the configurationManager class handle it or do I have to use XML to create the config file
Thanks a lot again for any replies
wavuti
Thanks a lot for all your help.
TishaL
http://msdn2.microsoft.com/en-us/library/system.configuration.configurationmanager.openmappedexeconfiguration.aspx
Best regards,
Johan Stenberg
Flavio Roberto
I would use the configuration manager/connectionstrings collection...
http://msdn2.microsoft.com/en-us/library/system.configuration.configurationmanager(VS.80).aspx
http://msdn2.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx
Best regards,
Johan Stenberg