如果应用程序在.NET 4.0上运行,那么获得通往.NET 2.0 Machine.config文件的路径的最佳方法是什么?

一种方法是进行字符串操作和文件系统访问,以替换v4.0*用v2.0* in new ConfigurationFileMap().MachineConfigFilename; 然后将其传递给 ConfigurationManager.OpenMappedMachineConfiguration(new ConfigurationFileMap(<HERE>)). 。如果没有更好的可用,我将诉诸该解决方案。

有帮助吗?

解决方案

由于我需要通往ASP.NET版本的Machine.config的路径,因此我不在乎所有.NET框架路径(例如3和3.5框架,因为它们只是2.0的扩展名)。我最终查询了 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET 注册表键和 Path 框架密钥的值。终于附加了 config\machine.config 到框架路径产生了预期的结果。

将ASP.NET运行时映射到Machine.config路径的方法将采用任何格式“ v2.0”,“ 2.0.50727.0”或仅仅是“ V2”和“ 2”的字符串“或一个第一位数,如果未指定小数位数为“ 2”,并从注册表中获得正确的值。类似的东西:


string runtimeVersion = "2.0";
string frameworkPath;
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\ASP.NET");
foreach (string childKeyName in regKey.GetSubKeyNames())
{
   if (Regex.IsMatch(childKeyName, runtimeVersion))
   {
       RegistryKey subKey = regKey.OpenSubKey(childKeyName))
       {
          frameworkPath = (string)subKey.GetValue("Path");
       }
   }
}
string machineConfigPath = Path.Combine(frameworkPath, @"config\machine.config");
string webRootConfigPath = Path.Combine(frameworkPath, @"config\web.config");

最后,我将此配置传递给WebConfigurationMap(我正在使用Microsoft.web.administration,但是您可以将其与System.Configuration一起使用,代码几乎相同):


using (ServerManager manager = new ServerManager())
{
   Configuration rootWebConfig = manager.GetWebConfiguration(new WebConfigurationMap(machineConfigPath, webRootConfigPath), null);
}

webconfigurationmap映射到自定义机器的配置。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top