c# - Trying to read web.config file for an IIS site from a Windows service -


i trying special web.config file web site installed on local iis. search windows service. following:

using (servermanager servermanager = new servermanager()) {     (int r = 0; r < servermanager.sites.count; r++)     {         string strsitename = servermanager.sites[r].name;         applicationcollection arrapps = servermanager.sites[r].applications;          (int = 0; < 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                 }              }         }      } 

where,

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; } 

and web.config file should recognized script this:

<?xml version="1.0"?> <!--   more information on how configure 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> 

but config.getsection("configuration"); throws exception:

{"filename: \\?\c:\inetpub\wwwroot\c:\users\dev\desktop\csharp\mytestwebapp\mytestwebapp\web.config\r\nerror: configuration section 'configuration' cannot read because missing section declaration\r\n\r\n"}

any idea how make work?

you need not target configuration, need target particular node need data. here example.

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(); }  

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -