I have developed an installer that will add a new website to IIS 7.5. Once the website is created in I would like to configure its connection strings. After the installation the look like this:
我开发了一个安装程序,它将为IIS 7.5添加一个新网站。在创建网站之后,我想配置它的连接字符串。安装后的外观如下:
<connectionStrings>
<add name="DefaultConnection" connectionString="$(ReplacableToken_DefaultConnection-Web.config Connection String_0)" providerName="System.Data.SqlClient" />
<add name="WebContext" connectionString="$(ReplacableToken_WebContext-Web.config Connection String_0)" providerName="System.Data.SqlClient" />
<add name="ActivityContext" connectionString="$(ReplacableToken_ActivityContext-Web.config Connection String_0)" providerName="System.Data.SqlClient" />
</connectionStrings>
I would like to configure each of them using C#. Here is what I tried:
我想使用c#对它们进行配置。以下是我的尝试:
using (ServerManager manager = new ServerManager())
{
Configuration webConfig = manager.GetWebConfiguration(myWebsite.name);
ConfigurationSection directoryBrowseSection = webConfig.GetSection("connectionStrings");
directoryBrowseSection.SetAttributeValue("DefaultConnection", ".\\SQLEXPRESS;Database=_usr;Integrated Security=true");
directoryBrowseSection.SetAttributeValue("WebContext", ".\\SQLEXPRESS;Database=_main;Integrated Security=true");
directoryBrowseSection.SetAttributeValue("ActivityContext", ".\\SQLEXPRESS;Database=_activity;Integrated Security=true");
manager.CommitChanges();
}
What I get: Exception Details: System.Runtime.InteropServices.COMException: Invalid index. (Exception from HRESULT: 0x80070585).
我得到的是:异常细节:System.Runtime.InteropServices。COMException:无效的指数。(从HRESULT例外:0 x80070585)。
The exeption is on the following line:
感受如下:
directoryBrowseSection.SetAttributeValue("DefaultConnection", ".\\SQLEXPRESS;Database=_usr;Integrated Security=true");
My question if not clear yet is how do I configure the connection strings on a website installed on IIS using Microsoft.web.administration?
如果还不清楚,我的问题是如何使用Microsoft.web.administration在IIS上安装的网站上配置连接字符串?
Any help appreciated
任何帮助表示赞赏
2 个解决方案
#1
1
I know this post is a little old, but I came across it while looking for something similar. Here's what I came up with:
我知道这篇文章有点旧,但我在寻找类似的东西时偶然发现了它。以下是我的想法:
//using Microsoft.Web.Administration;
var file = "PATH TO FILE";
var configFile = new FileInfo(file);
var virtualDirectoryMapping = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
var webConfigFileMap = new WebConfigurationFileMap();
webConfigFileMap.VirtualDirectories.Add("/", virtualDirectoryMapping);
var webConfig = System.Web.Configuration.WebConfigurationManager.OpenMappedWebConfiguration(webConfigFileMap, "/");
webConfig.ConnectionStrings.ConnectionStrings.Add(new System.Configuration.ConnectionStringSettings("NAME", "CONNECTION STRING"));
webConfig.Save();
In case these help anyone else out there that needs to do anything with a .NET config file (Get the Configuration, pull appSettings or ConnectionString sections):
如果这些帮助了其他任何需要使用。net配置文件做任何事情的人(获取配置、拉出appSettings或ConnectionString节):
using System.Configuration;
using System.IO;
using System.Web.Configuration;
namespace YourNamespace
{
public static class DotNetConfigFile
{
public static Configuration GetConfiguration(string filePath)
{
if (File.Exists(filePath))
{
FileInfo file = new FileInfo(filePath);
if (file.Name.ToLower() != "web.config")
{
try
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap() { ExeConfigFilename = filePath };
return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}
catch (ConfigurationErrorsException ex)
{
return null;
//throw;
}
}
else
{
try
{
var virtualDirectoryMapping = new VirtualDirectoryMapping(file.DirectoryName, true, file.Name);
var webConfigFileMap = new WebConfigurationFileMap();
webConfigFileMap.VirtualDirectories.Add("/", virtualDirectoryMapping);
return WebConfigurationManager.OpenMappedWebConfiguration(webConfigFileMap, "/");
}
catch(ConfigurationErrorsException ex)
{
return null;
//throw;
}
}
}
else
throw new FileNotFoundException("File not found", filePath);
}
public static KeyValueConfigurationCollection GetAppSettings(string filePath)
{
var config = GetConfiguration(filePath);
if (config != null)
return config.AppSettings.Settings;
else
return null;
}
public static KeyValueConfigurationCollection GetAppSettings(FileInfo fileInfo)
{
return GetAppSettings(fileInfo.FullName);
}
public static ConnectionStringSettingsCollection GetConnectionStrings(string filePath)
{
var config = GetConfiguration(filePath);
if (config != null)
return config.ConnectionStrings.ConnectionStrings;
else
return null;
}
public static ConnectionStringSettingsCollection GetConnectionStrings(FileInfo fileInfo)
{
return GetConnectionStrings(fileInfo.FullName);
}
}
#2
0
Given the lack of answers and the fact that I could not find a way to do it using microsoft.web.administration, I resorted to reading Web.config from its phyical path. This way I could configure any element I needed in the following fashion:
由于没有答案,而且我无法找到使用microsoft.web的方法。管理,我求助于阅读网络。从它的系统路径配置。通过这种方式,我可以按照以下方式配置所需的任何元素:
string[] file = Directory.GetFiles(myWebsite.physicalPath, "*config");
if (file != null)
{
XmlDocument doc = new XmlDocument();
doc.Load(file[1]);
XmlNodeList configurationStrings = doc.SelectNodes("/configuration/connectionStrings/add");
configurationStrings[0].Attributes["connectionString"].Value = ".\\SQLEXPRESS;Database=_usr;Integrated Security=true";
configurationStrings[1].Attributes["connectionString"].Value = ".\\SQLEXPRESS;Database=_main;Integrated Security=true";
configurationStrings[2].Attributes["connectionString"].Value = ".\\SQLEXPRESS;Database=_activity;Integrated Security=true";
doc.Save(file[1]);
}
#1
1
I know this post is a little old, but I came across it while looking for something similar. Here's what I came up with:
我知道这篇文章有点旧,但我在寻找类似的东西时偶然发现了它。以下是我的想法:
//using Microsoft.Web.Administration;
var file = "PATH TO FILE";
var configFile = new FileInfo(file);
var virtualDirectoryMapping = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
var webConfigFileMap = new WebConfigurationFileMap();
webConfigFileMap.VirtualDirectories.Add("/", virtualDirectoryMapping);
var webConfig = System.Web.Configuration.WebConfigurationManager.OpenMappedWebConfiguration(webConfigFileMap, "/");
webConfig.ConnectionStrings.ConnectionStrings.Add(new System.Configuration.ConnectionStringSettings("NAME", "CONNECTION STRING"));
webConfig.Save();
In case these help anyone else out there that needs to do anything with a .NET config file (Get the Configuration, pull appSettings or ConnectionString sections):
如果这些帮助了其他任何需要使用。net配置文件做任何事情的人(获取配置、拉出appSettings或ConnectionString节):
using System.Configuration;
using System.IO;
using System.Web.Configuration;
namespace YourNamespace
{
public static class DotNetConfigFile
{
public static Configuration GetConfiguration(string filePath)
{
if (File.Exists(filePath))
{
FileInfo file = new FileInfo(filePath);
if (file.Name.ToLower() != "web.config")
{
try
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap() { ExeConfigFilename = filePath };
return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}
catch (ConfigurationErrorsException ex)
{
return null;
//throw;
}
}
else
{
try
{
var virtualDirectoryMapping = new VirtualDirectoryMapping(file.DirectoryName, true, file.Name);
var webConfigFileMap = new WebConfigurationFileMap();
webConfigFileMap.VirtualDirectories.Add("/", virtualDirectoryMapping);
return WebConfigurationManager.OpenMappedWebConfiguration(webConfigFileMap, "/");
}
catch(ConfigurationErrorsException ex)
{
return null;
//throw;
}
}
}
else
throw new FileNotFoundException("File not found", filePath);
}
public static KeyValueConfigurationCollection GetAppSettings(string filePath)
{
var config = GetConfiguration(filePath);
if (config != null)
return config.AppSettings.Settings;
else
return null;
}
public static KeyValueConfigurationCollection GetAppSettings(FileInfo fileInfo)
{
return GetAppSettings(fileInfo.FullName);
}
public static ConnectionStringSettingsCollection GetConnectionStrings(string filePath)
{
var config = GetConfiguration(filePath);
if (config != null)
return config.ConnectionStrings.ConnectionStrings;
else
return null;
}
public static ConnectionStringSettingsCollection GetConnectionStrings(FileInfo fileInfo)
{
return GetConnectionStrings(fileInfo.FullName);
}
}
#2
0
Given the lack of answers and the fact that I could not find a way to do it using microsoft.web.administration, I resorted to reading Web.config from its phyical path. This way I could configure any element I needed in the following fashion:
由于没有答案,而且我无法找到使用microsoft.web的方法。管理,我求助于阅读网络。从它的系统路径配置。通过这种方式,我可以按照以下方式配置所需的任何元素:
string[] file = Directory.GetFiles(myWebsite.physicalPath, "*config");
if (file != null)
{
XmlDocument doc = new XmlDocument();
doc.Load(file[1]);
XmlNodeList configurationStrings = doc.SelectNodes("/configuration/connectionStrings/add");
configurationStrings[0].Attributes["connectionString"].Value = ".\\SQLEXPRESS;Database=_usr;Integrated Security=true";
configurationStrings[1].Attributes["connectionString"].Value = ".\\SQLEXPRESS;Database=_main;Integrated Security=true";
configurationStrings[2].Attributes["connectionString"].Value = ".\\SQLEXPRESS;Database=_activity;Integrated Security=true";
doc.Save(file[1]);
}