Windows Develop Bookmark and Share   
 index > ClickOnce and Setup & Deployment Projects > saving a connection string in app.config
 

saving a connection string in app.config

I want a setup project that saves the connection string the user selects via the mdac dialogs into the application configuration file.

I managed to get the following code :

public override void Install(System.Collections.IDictionary stateSaver)

{

base.Install(stateSaver);

MSDASC.DataLinks udl = new MSDASC.DataLinksClass();

ADODB.Connection conn = (ADODB.Connection)udl.PromptNew();

if (conn != null)

{

string ConnectionString = conn.ConnectionString;

string[] ConnectionStringParts = ConnectionString.Split(';');

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

foreach (string ConnectionStringPart in ConnectionStringParts)

{

string[] KeyValue = ConnectionStringPart.Split('=');

try

{

builder.Add(KeyValue[0], KeyValue[1]);

}

catch (Exception)

{

}

}

MessageBox.Show(builder.ConnectionString);

//now i need to figure out to save this stirng in the configuration file

//what is the path ? can I get to it via Context ?

//can I use the Configuration class ?

}

}

Can anyone help with the last part where I need to save the string in my application.config file.

stuyckp  Tuesday, March 21, 2006 9:22 PM

Yes. You should be able to use the configuration classes.

// Get the current configuration file.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

// Get the section.

ConfigurationSection section = config.GetSection("mysection");

// Save the section.
config.Save();

philipsh  Tuesday, March 21, 2006 9:52 PM

sorry this does not work, I guess

System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

is the problem. Does this statement give access to the configuration file of the application you are installing or to the configurationfile of the setup.exe ?

Now openexeconfiguration supports a filename as well. But how do I know where the setup is installing, what is the path the user selected for installation ?

Philip Stuyck  Wednesday, March 22, 2006 5:42 PM

Not sure if this is the best way but this is what I do; pass the install target directory to a custom actions project via the CustomActionData property like this:

/WebRoot="[TARGETDIR]\"

The MSI TARGETDIR is the directory the app was installed into. Then in the custom action project it can be accessed and used to get the config file like this:

string sConfigFile = Context.Parameters["WebRoot"].ToString() + "Web.config";   

System.IO.FileInfo oFileInfo = new System.IO.FileInfo(sConfigFile);
XmlNode oNode;

if (oFileInfo.Exists)
{
    //Loads the config file into the XML DOM.
    XmlDocument oMyXMLDoc = new XmlDocument();
    oMyXMLDoc.Load(oFileInfo.FullName);

     oNode = oMyXMLDoc.DocumentElement.SelectSingleNode("//whatever....");

    //modify xml nodes....

    // Save changes

   oMyXMLDoc.Save(oFileInfo.FullName);
}
else
{
   throw (new InstallException("Error updating web configuration file. Missing file: " + oFileInfo.FullName));
}

 

philipsh  Monday, April 03, 2006 6:41 PM
"But how do I know where the setup is installing, what is the path the user selected for installation ?"

Application.StartupPath

Therefore to use the ConfigurationManager.OpenExeConfiguration 2nd overloaded method you'd just parse in:

Application.StartupPath + "\\appName.exe.config"

Or

AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

For ASP.Net follow Phils advice (above), for Winforms this might help...





Public Shared Sub UpdateAppConfigSetting(ByVal ConfigSection As String, ByVal KeyName As String, ByVal KeyValue As String)

'ConfigSection = "appSettings" or "connectionStrings"


Dim XmlDoc As New XmlDocument()
XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)

' Navigate Each xml Element of app.Config file
        For Each xElement As XmlElement In XmlDoc.DocumentElement
If xElement.Name = ConfigSection Then
For Each xNode As XmlNode In xElement.ChildNodes
If xNode.Attributes(0).Value = KeyName Then

xNode.Attributes(1).Value = KeyValue
GoTo lnUpdated
                    End If
Next
End If
Next
lnUpdated:

' Save app.config file
XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)

End Sub

Meaning Of Lights  Sunday, September 09, 2007 7:23 AM

You can use google to search for other answers

Custom Search

More Threads

• Click once Error
• MSI Issue - ReInstall app for every login User
• UnauthorizedAccessException in Windows Vista
• Custom actions not running on a particular machine
• What do I need to do to deploy controls that target Windows XP Themes?
• two click once profiles
• How to configure production server to host ClickOnce deployment site?
• .NET frame work and windows installer prerequistes
• Requested Registry access is not allowed
• Deploying custom client configuration files with click once deployment.