webservice config 问题 来着有分

美丽大道 2009-11-18 12:34:36
我做了webservice ,用 虚拟路径 现在放在IIS中一个80端口的网站下面,这个webservice有web.config文件。另外我要对文件里面读写操作。但是我无法获得web.config的对象 ,用下面的代码:



config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");


这个OpenWebConfiguration 方法里面的参数我填了很多试了,都只能获得主网站config,无法获得我这个虚拟目录中webservice的config,请教高手,怎么才能获得这个config。!
...全文
256 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
美丽大道 2009-12-05
  • 打赏
  • 举报
回复
[Quote=引用 24 楼 photoplan 的回复:]
引用 21 楼 unstopable 的回复:
引用 20 楼 photoplan 的回复:
WebConfigurationManager.OpenWebConfiguration("~/web.config")


这个方法不行啊。。。。


读到的配置文件不是你要的?
[/Quote]


不是啊。。。。。 我的虚拟路径配制在 sharepoint的网站下面,这个网站本身有个config。 他都读到那个config了。。。。

孤芳-自赏 2009-12-04
  • 打赏
  • 举报
回复
up
liang4571231 2009-12-04
  • 打赏
  • 举报
回复
WebConfigurationManager.OpenWebConfiguration(System.Web.UI.Page.Server.MapPath(@"../../web.config"));
用虚拟路径吧
美丽大道 2009-12-04
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 photoplan 的回复:]
WebConfigurationManager.OpenWebConfiguration("~/web.config")
[/Quote]

这个方法不行啊。。。。
photoplan 2009-12-04
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 unstopable 的回复:]
引用 20 楼 photoplan 的回复:
WebConfigurationManager.OpenWebConfiguration("~/web.config")


这个方法不行啊。。。。
[/Quote]

读到的配置文件不是你要的?
photoplan 2009-12-03
  • 打赏
  • 举报
回复
WebConfigurationManager.OpenWebConfiguration("~/web.config")
hgx54599 2009-12-03
  • 打赏
  • 举报
回复
恩 学习了
ProjectDD 2009-12-03
  • 打赏
  • 举报
回复
垂子哦,先顶了再说。。
十八道胡同 2009-12-03
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 sxmonsy 的回复:]
10 11楼正解,
[/Quote]
up
美丽大道 2009-12-03
  • 打赏
  • 举报
回复
为什么我用了楼上的方法试验了。还是会获得他网站本身的web。config 而不是这个虚拟路径下面的web.config
sxmonsy 2009-11-20
  • 打赏
  • 举报
回复
10 11楼正解,
angel6709 2009-11-20
  • 打赏
  • 举报
回复
学习,lz,我发布的webservece 为什么在WinApp中找不到
conan19771130 2009-11-20
  • 打赏
  • 举报
回复
mark
huxili 2009-11-20
  • 打赏
  • 举报
回复
学习先
宝_爸 2009-11-20
  • 打赏
  • 举报
回复
msdn上的另一篇文章:http://msdn.microsoft.com/en-us/library/ms228063.aspx
我将重要内容标志成红色了。

Opening a Configuration Object Mapped to Web-Application Configuration Settings
To open the configuration file for a Web application, your application calls the OpenWebConfiguration static method of the WebConfigurationManager class, passing the relative path of the Internet Information Services (IIS) virtual directory to open.

The values of the path parameter can be obtained from the IIS metabase path of the directory that contains the required configuration. For example, if the IIS metabase path is W3SVC/1/Root/Temp, then the path is /Temp because the default site is 1.

In the following code example, the OpenWebConfiguration method opens and returns the configuration object that corresponds to the Temp Web application in the default Web site. The configuration object includes configuration settings that are specified locally in a Web.config file and all inherited settings from parent configuration files, all the way back to the Machine.config file. Other versions of this overloaded method allow you to specify the Web site, a location, a remote server, and user information.

            // Obtains the configuration settings for a Web application.
System.Configuration.Configuration webConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Temp");
webConfig.SaveAs("c:\\webConfig.xml");


It is possible to open a System.Configuration..::.Configuration object even when a physical Web.config file does not exist. In this case, the returned configuration consists entirely of settings that are inherited from the configuration file hierarchy. For more information, see ASP.NET Configuration Scenarios.

Required Privileges
To open a Web configuration file, the application needs Read permission for the physical Web.config file, and for all of its parent files in the hierarchy.

宝_爸 2009-11-20
  • 打赏
  • 举报
回复
msdn中的解释
public static Configuration OpenWebConfiguration (
string path
)

Parameters
path
The virtual path to the configuration file. If a null reference (Nothing in Visual Basic), the root Web.config file is opened.

例子:configTest应该是虚拟目录

// Show how to use OpenWebConfiguration(string).
// It gets he appSettings section of a Web application
// runnig on the local server.
static void OpenWebConfiguration1()
{
// Get the configuration object for a Web application
// running on the local server.
System.Configuration.Configuration config =
WebConfigurationManager.OpenWebConfiguration("/configTest")
as System.Configuration.Configuration;

// Get the appSettings.
KeyValueConfigurationCollection appSettings =
config.AppSettings.Settings;


// Loop through the collection and
// display the appSettings key, value pairs.
Console.WriteLine("[appSettings for app at: {0}]", "/configTest");
foreach (string key in appSettings.AllKeys)
{
Console.WriteLine("Name: {0} Value: {1}",
key, appSettings[key].Value);
}

Console.WriteLine();
}


wapit 2009-11-20
  • 打赏
  • 举报
回复
没用过。
不过感觉WebConfigurationManager.OpenWebConfiguration("/web.config")能取到根目录的话……试试这个:
WebConfigurationManager.OpenWebConfiguration("~/web.config")
ixhxbread 2009-11-20
  • 打赏
  • 举报
回复
前辈我没用过OpenWebConfiguration 但我是用下面这种方式读的,给你参考一下
string conn = System.Configuration.ConfigurationManager.ConnectionStrings["111"].ToString();

<connectionStrings>
<add name="111" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\user\\Desktop\\task.accdb"/>
</connectionStrings>
美丽大道 2009-11-19
  • 打赏
  • 举报
回复
有高手么。。过来帮帮忙被~~
suiyuan110 2009-11-18
  • 打赏
  • 举报
回复
呵呵~~暂时还没用过呢。。以后或许能用到,学习ing!!
up!!!!
加载更多回复(5)

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧