web.config中添加appSettings 的key如下<configuration>
  <appSettings>
    <add key="key1" value="abcd" />
  </appSettings>
</configuration>
程序中如此访问key
ConfigurationSettings.AppSettings("key1")

解决方案 »

  1.   

    这个方法没有用在设计时,这个的返回值是null.
      

  2.   

    才看明白,你的要求是设计时。
    web.config本身是程序运行时的配置文件,在设计时没有被读取,所以肯定读不出来了。你读它的目的呢?
      

  3.   

    我自己写了一个控件,需要在设计的时候读入一些信息,这个信息是开发人员指定的webService的地址,然后我的控件就根据这个地址调用webService,然后例出一些信息供开发人员选择。
    那么在设计的时候能知道你所设计的页面的物理地址吗?
      

  4.   

    using System.Configuration
    ConfigurationSettings.AppSettings["key1"]
      

  5.   

    内容:
    <configuration>  
    <system.web>
    <compilation debug="true"/>
    </system.web>
    <appSettings>
    <add key="MM_CONNECTION_STRING_aa" value="aaaaaaaaaaaa" />
    </appSettings>
        <system.web>
            <customErrors mode="Off"/>
        </system.web>
    </configuration>
    读取:
     Public str As String = System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_aa")
      

  6.   

    如果你的config文件是放在默认的目录下的话用
    (string)System.Configuration.ConfigurationSettings.AppSettings["configName"]就可以取到值,
     在你的*.csproj文件里中
       <File
                  RelPath = "Web.config"
                   BuildAction = "Content"
        />配置了你的webconfig的路径
      

  7.   

    web.config is not loaded at design-time, so you cannot retrieve the values with System.Configuration.ConfigurationSettings.AppSettingstry to get the path of the assembly and find where the web.config is located public override object EditValue(
                ITypeDescriptorContext context,
                IServiceProvider provider,
                object value ) {            object serviceID = provider.GetService( typeof(ITypeResolutionService) );
                ITypeResolutionService resolver = (ITypeResolutionService)provider.GetService( typeof(ITypeResolutionService) );            AssemblyName assemblyName = serviceID.GetType().Assembly.GetName();            string strPathName  = resolver.GetPathOfAssembly( assemblyName );
                //.......
            }then load the web.config with classes in System.Xml
      

  8.   

    思归:
    这样做是很有道理,但是为什么我按照你的方法做得到的strPathName是null?
      

  9.   

    it is too bad, the design-time support is so poor, but see
    http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=Kh9YAeZXCHA.2428%40cpmsftngxa07
      

  10.   

    今天又尝试了一下思归说的方法发现strPathName的值不再是null,而是:
    C:\Documents and Settings\Administrator\VSWebCache\WANG-WEI\Test\obj\Debug\Test.dll
    这并不是我想要的是不是这一句 
    object serviceID = provider.GetService( typeof(ITypeResolutionService) ); 
    写得有问题,请思归请教。
    谢谢!
    这个问题已经困扰我好多天了。
      

  11.   

    what path do you need? "C:\Documents and Settings\Administrator\VSWebCache\WANG-WEI\Test\obj"? then use System.IO.Path.GetDirectoryName
      

  12.   

    我想得到的是Web.config的地址。
    应该是c:\inetpub\wwwroot\Test\。
      

  13.   

    then the only choice is probably to use VisualStudio.DTE, see the code in the above link