Click here to Skip to main content
12,500,915 members (50,133 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
Hi Experts,

I am searching for a way to edit app.config connection strings using .msi file. Since it needs to be installed by other people. I dont want others to explicitly go into file and edit the server address and db username and password.

I want to have a nice interface or an .msi file would be a very good option. Can someone guide me with a starting point on this issue.

Your help is highly appreciated.
Posted 19-Nov-12 20:30pm

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

If you are able to change the app.config file at run time here is a snippet to do so.

//Usage
WriteSettings("KeyInAppConfig", "NewValues");
//This will find the KeyInAppConfig setting in app.config and set the value of it to NewValues

private static XmlDocument loadConfigDocument()
{
	XmlDocument doc = null;
	try
	{
		doc = new XmlDocument();
		doc.Load(getConfigFilePath());
		return doc;
	}
	catch (System.IO.FileNotFoundException e)
	{
		throw new Exception("No config filed found.", e);
	}
}
 
private static string getConfigFilePath()
{
	return Assembly.GetExecutingAssembly().Location + ".config";
}
 

public static void WriteSetting(string key, string value)
{
	XmlDocument doc = loadConfigDocument();
	XmlNode node = doc.SelectSingleNode("//appSettings");
 
	if (node == null)
		throw new InvalidOperationException("appSettings section not found in config file.");
 
	try
	{
		XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
 
		if (elem != null)
		{
			elem.SetAttribute("value", value);
		}
		else
		{
			elem = doc.CreateElement("add");
			elem.SetAttribute("key", key);
			elem.SetAttribute("value", value);
			node.AppendChild(elem);
		}
		doc.Save(getConfigFilePath());
	}
	catch
	{
		throw;
	}
}

Hope it helps
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 13 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100