尝试从Windows服务读取IIS站点的web.config文件 [英] Trying to read web.config file for an IIS site from a Windows service

查看:1358
本文介绍了尝试从Windows服务读取IIS站点的web.config文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为本地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:


{文件名:
\\?\ C: \inetpub \wwwroot \ C:\ Users \ Dev \Desktop \CSharp \ MyTestWebApp \ MyTestWebApp \web.config \\\\ nnError:
配置部分'配置'无法阅读,因为
缺少一个部分声明\\\\\ n}

{"Filename: \\?\C:\inetpub\wwwroot\C:\Users\Dev\Desktop\CSharp\MyTestWebApp\MyTestWebApp\web.config\r\nError: The configuration section 'configuration' cannot be read because it is missing a section declaration\r\n\r\n"}

知道如何让它工作吗?

推荐答案

我感觉,如果你有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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆