我开发了一个安装程序,它将向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>
我想使用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();
}
我得到了什么: 异常详细信息:System.Runtime.InteropServices.COMException:索引无效。 (HRESULT异常:0x80070585)。
例外情况如下:
directoryBrowseSection.SetAttributeValue("DefaultConnection", ".\\SQLEXPRESS;Database=_usr;Integrated Security=true");
我的问题,如果还不清楚,我如何使用Microsoft.web.administration在IIS上安装的网站上配置连接字符串?
答案 0 :(得分:2)
我知道这篇文章有点旧,但我在寻找类似的东西时遇到过它。这就是我想出的:
//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();
如果这些帮助那些需要对.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);
}
}
答案 1 :(得分:0)
由于缺乏答案以及我无法使用microsoft.web.administration找到解决方法的事实,我从其物理路径中读取了Web.config。这样我就可以按以下方式配置我需要的任何元素:
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]);
}