I do this with a custom task that does the same thing as your code, but MSBuidl becomes:
<UpdateConnectionString ConfigFile ="path\to\web.config"
ConnectionStringName="MyConnectionStringName"
ConnectionString="connection-string-here"/>
The code for this task:
public class UpdateConnectionString : Task
{
[Required]
public string ConfigFile { get; set; }
[Required]
public string ConnectionStringName { get; set; }
[Required]
public string ConnectionString { get; set; }
public override bool Execute()
{
try
{
var fi = new FileInfo(ConfigFile);
if(!fi.Exists)
{
Log.LogError("File {0} does not exist");
return false;
}
fi.IsReadOnly = false;
XDocument doc = XDocument.Load(ConfigFile);
var confignode = doc.Descendants("configuration").Single();
var connectionStrings = confignode.Descendants("connectionStrings").SingleOrDefault();
if(connectionStrings == null)
{
connectionStrings = new XElement("connectionStrings");
confignode.Add(connectionStrings);
}
var connectionElement = connectionStrings.Descendants("add").SingleOrDefault(
e => e.Attribute("name") != null &&
string.Compare(e.Attribute("name").Value, ConnectionStringName,
StringComparison.OrdinalIgnoreCase) == 0);
if (connectionElement == null)
{
connectionElement = new XElement("add", new XAttribute("name", ConnectionStringName));
connectionStrings.Add(connectionElement);
}
connectionElement.SetAttributeValue("connectionString", ConnectionString);
doc.Save(ConfigFile);
}
catch (Exception ex)
{
Log.LogErrorFromException(ex, true);
return false;
}
return true;
}
}
Please note that this code will also add a connection string if it is not there, it is probably more complicated than you need right now.
source
share