问题描述
我正在尝试为本地IIS上安装的网站查找特殊的 web.config
文件。我从Windows服务进行搜索。我执行以下操作:
I am trying to look for a special web.config
file for a web site installed on a local IIS. I do this search from a Windows service. I do the following:
using (ServerManager serverManager = new ServerManager())
{
for (int r = 0; r < serverManager.Sites.Count; r++)
{
string strSiteName = serverManager.Sites[r].Name;
ApplicationCollection arrApps = serverManager.Sites[r].Applications;
for (int a = 0; a < arrApps.Count; a++)
{
Microsoft.Web.Administration.Application aa = arrApps[a];
foreach (VirtualDirectory vd2 in aa.VirtualDirectories)
{
string strPhysPath = Environment.ExpandEnvironmentVariables(vd2.PhysicalPath);
int rr = 0;
try
{
Configuration cnfg = serverManager.GetWebConfiguration(strSiteName, strPhysPath);
if (cnfg != null)
{
string swww = getWebConfigGeneralParamValue(cnfg, "SpecNodeName");
}
}
catch
{
//Error
}
}
}
}
其中,
public static string getWebConfigGeneralParamValue(Configuration config, string strParamKey)
{
string strRes = null;
try
{
ConfigurationSection configSection1 = config.GetSection("configuration");
if (configSection1 != null)
{
ConfigurationElement configGeneralParams = configSection1.GetChildElement("GeneralParams");
if (configGeneralParams != null)
{
ConfigurationElementCollection configCol = configSection1.GetCollection();
if (configCol != null)
{
strRes = configCol[strParamKey].ToString();
}
}
}
}
catch(Exception ex)
{
strRes = null;
}
return strRes;
}
并且此脚本应该识别的web.config文件类似于这个:
and the web.config file that should be recognized by this script is something like this:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<!-- regular ASP.NET config stuff -->
<GeneralParams>
<param key="SpecNodeName" value="Special Node Value"/>
</GeneralParams>
</configuration>
但我得到的是 config.GetSection(configuration);
抛出此异常:
But what I get is that config.GetSection("configuration");
throws this exception:
知道如何让它工作吗?
推荐答案
我感觉,如果你有web.config为什么要使用ServerManager 。
看看下面的代码,这个我一直在我的代码中使用,它作为一个魅力。
它的作用是以可读格式加载web.config。
I am feeling, if you have web.config why you want to use ServerManager.Take a look at below code, this I have been using in my code and it works as a charm.What it does is it loads web.config in readable format.
var assemblyPath = Path.GetDirectoryName(typeof (ConfigurationTests).Assembly.Location);
string webConfigPath = MyPath;
string directory = System.IO.Path.GetFullPath(Path.Combine(assemblyPath,webConfigPath));
Console.WriteLine(directory);
Assert.That(Directory.Exists(directory));
VirtualDirectoryMapping vdm = new VirtualDirectoryMapping(directory, true);
WebConfigurationFileMap wcfm = new WebConfigurationFileMap();
wcfm.VirtualDirectories.Add("/", vdm);
System.Configuration.Configuration webConfig = WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
var connectionString = webConfig.ConnectionStrings.ConnectionStrings["ConnectionString"].ConnectionString;
Console.WriteLine("Connection String:");
Console.WriteLine(connectionString);
Console.WriteLine("AppSettings:");
foreach (var key in webConfig.AppSettings.Settings.AllKeys)
{
Console.WriteLine("{0} : {1}", key, webConfig.AppSettings.Settings[key].Value);
}
这篇关于尝试从Windows服务读取IIS站点的web.config文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!