我做了webservice ,用 虚拟路径  现在放在IIS中一个80端口的网站下面,这个webservice有web.config文件。另外我要对文件里面读写操作。但是我无法获得web.config的对象 ,用下面的代码:     config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
这个OpenWebConfiguration 方法里面的参数我填了很多试了,都只能获得主网站config,无法获得我这个虚拟目录中webservice的config,请教高手,怎么才能获得这个config。!

解决方案 »

  1.   

    WebConfigurationManager.OpenWebConfiguration("/web.config") 这样获不到吗,看看是不路径放的不对啊?
      

  2.   

    呵呵~~暂时还没用过呢。。以后或许能用到,学习ing!!
    up!!!!
      

  3.   


    不行啊,还是读的D:\Inetpub\wwwroot\wss\VirtualDirectories\80\web.config 这个地址。我的config和我的asmx文件是在一起的~
      

  4.   

    前辈我没用过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>
      

  5.   

    没用过。
    不过感觉WebConfigurationManager.OpenWebConfiguration("/web.config")能取到根目录的话……试试这个:
    WebConfigurationManager.OpenWebConfiguration("~/web.config") 
      

  6.   

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

  7.   

    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.
      

  8.   

    学习,lz,我发布的webservece 为什么在WinApp中找不到
      

  9.   

    为什么我用了楼上的方法试验了。还是会获得他网站本身的web。config 而不是这个虚拟路径下面的web.config
      

  10.   

    WebConfigurationManager.OpenWebConfiguration("~/web.config") 
      

  11.   

    WebConfigurationManager.OpenWebConfiguration(System.Web.UI.Page.Server.MapPath(@"../../web.config"));
    用虚拟路径吧
      

  12.   

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