如何使用物理路径打开WebConfiguration? [英] How to OpenWebConfiguration with physical path?

查看:118
本文介绍了如何使用物理路径打开WebConfiguration?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个获胜表格,可以在IIS7中创建一个站点。
一个功能需要打开web.config文件并进行一些更新。 (连接字符串,smtp,模拟)

I have a win form that creates a site in IIS7. One function needs to open the web.config file and make a few updates. (connection string, smtp, impersonation)

但是我没有虚拟路径,只有物理路径。

However I do not have the virtual path, just the physical path.

是否仍然可以使用WebConfigurationManager?

Is there any way I can still use WebConfigurationManager?

我需要使用它的功能来查找节并进行读取/写入。

I need to use it's ability to find section and read/write.

System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration


推荐答案

您将必须将physicalPath映射到virtualPath。

You will have to map the physicalPath to a virtualPath. Here is how you would do that.

using System.Web.Configuration;  //Reference the System.Web DLL (project needs to be using .Net 4.0 full, not client framework)

public static Configuration OpenConfigFile(string configPath)
{
    var configFile = new FileInfo(configPath);
    var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
    var wcfm = new WebConfigurationFileMap();
    wcfm.VirtualDirectories.Add("/", vdm);
    return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");
}

这篇关于如何使用物理路径打开WebConfiguration?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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