Use your custom build environment variable support:
Let you have:
<connectionStrings>
<add name="My" connectionString="..;Data Source=|%AppData%|Entities.sdf;.." />
</connectionStrings>
The you can use:
using System.Configuration; // requires reference to System.Configuration.dll
ConfigurationManager.ConnectionStrings["EntityConnectionString"].ConnectionString.Replace("%AppData%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
Next way you can support several environment variables:
var vars = new Dictionary<string, string>
{
{ "%AppData%", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
{ "%Temp%", Environment.GetFolderPath(SpecialFolder.Temp) },
// etc..
{ "%YourNonStandardVar", "YourNonStandartPath" }
};
var result = ConfigurationManager.ConnectionStrings["YourString"].ConnectionString
foreach (var v in vars)
result = result.Replace(v.Key, v.Value);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…