2013-04-05 53 views
2

我試圖尋找一個特殊的web.config文件安裝在本地IIS網站的IIS站點。我從Windows服務中執行此搜索。我做到以下幾點:試圖讀取web.config文件從Windows服務

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文件是這樣的:

<?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");拋出此異常:

{"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"}

不知道如何使它發揮作用?

回答

0

你不必爲目標的配置,你需要的目標,從那裏你需要得到你的數據的特定節點。這裏是例子。

using(ServerManager mgr = ServerManager.OpenRemote("Some-Server")) { 

    Configuration config = mgr.GetWebConfiguration("site-name", "/test-application"); 

    ConfigurationSection appSettingsSection = config.GetSection("appSettings"); 

    ConfigurationElementCollection appSettingsCollection = appSettingsSection.GetCollection(); 

    ConfigurationElement addElement = appSettingsCollection.CreateElement("add"); 
    addElement["key"] = @"NewSetting1"; 
    addElement["value"] = @"SomeValue"; 
    appSettingsCollection.Add(addElement); 

    serverManager.CommitChanges(); 
} 
+0

感謝。現在,在更正'GetWebConfiguration'中的路徑後,我仍然遇到異常情況:'配置節'configuration'無法被讀取,因爲它缺少一個節聲明。 – c00000fd 2013-04-05 05:51:48

0

我感覺,如果你有web.config你爲什麼要使用ServerManager。 看看下面的代碼,這個我一直在用我的代碼,它可以作爲一個魅力。 它所做的是以可讀格式加載web.config。

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); 
      } 
+0

謝謝。我最終只是使用'XmlTextReader'解析web.config。編寫需要一點時間,但至少你知道發生了什麼,這與所有內置的解析類非常混淆。 – c00000fd 2013-04-07 01:05:49