Click here to Skip to main content
16,011,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

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

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


C#
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900