最好的方式来获得一个路径的Machine.config不同的.NET版本 [英] The best way to get a path to machine.config of a different .NET version

查看:163
本文介绍了最好的方式来获得一个路径的Machine.config不同的.NET版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我最好的方式得到一个路径.NET 2.0 machine.config文件,如果应用程序是在.NET 4.0上运行?

Whats the best way to get a path to .net 2.0 machine.config file, if the application is running on .net 4.0?

一种方法是做字符串操作和文件系统访问来替换V4.0 *在
新ConfigurationFileMap()MachineConfigFilename 2.0。*; ,然后将它传递给 ConfigurationManager.OpenMappedMachineConfiguration(新ConfigurationFileMap(LT;此处>))。我将诉诸这一解决方案,如果没有更好的是可用的。

One way would be to do string manipulation and file system access to replace v4.0* with v2.0* in new ConfigurationFileMap().MachineConfigFilename; and then pass it to ConfigurationManager.OpenMappedMachineConfiguration(new ConfigurationFileMap(<HERE>)). I will resort to this solution if nothing better is available.

推荐答案

由于我所需要的路径,为的Machine.config ASP.NET版本,我也没在意所有.NET框架的路径(例如3和3.5框架,因为它们都只是2.0扩展)。我结束了查询框架 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET 注册表键和路径价值键。最后追加 config\machine.config 来的框架路径取得了预期的效果。

Since I needed the path to machine.config for ASP.NET versions, I didn't care about all .NET framework paths (e.g. 3 and 3.5 frameworks since they are just extensions of 2.0). I ended up querying HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET registry key and Path value of the framework key. Finally appending config\machine.config to the framework path yielded desired results.

方法映射ASP。网运行时的Machine.config路径将采取任何格式2.0的字符串,2.0.50727.0或者第2版和2,正则表达式它任一个十进制数,如2.0或一个第一数字如果小数位数中未指定,如2,并从注册表中的正确的价值。一些与此类似:

The method to map ASP.NET runtime to machine.config path would take strings of any format "v2.0", "2.0.50727.0" or just "v2" and "2", regex it to either one decimal digit like "2.0" or one first digit if decimal digits were not specified like "2" and get the right value from the registry. Something similar to this:


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");



最后,我通过这个CONFIGS到WebConfigurationMap(我使用Microsoft.Web.Administration,但你可以与System.Configuration使用它,以及,代码几乎是一样的):

Lastly, I pass this configs to WebConfigurationMap (I am using Microsoft.Web.Administration, but you can use it with System.Configuration as well, the code is almost the same):


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



WebConfigurationMap映射配置定制的machine.config和根网站。配置(因此空在GetWebConfiguration第二个参数())

WebConfigurationMap maps configuration to custom machine.config and root web.config (hence null as a second argument in GetWebConfiguration())

这篇关于最好的方式来获得一个路径的Machine.config不同的.NET版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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