代码之家  ›  专栏  ›  技术社区  ›  kobe

Visual Studio生成后事件无法访问appsettings或webconfig

  •  0
  • kobe  · 技术社区  · 14 年前

    我有以下代码。

    我在后期生成事件中执行此操作,下面的类正在执行,但它无法对appsettings进行任何访问。

    我尝试了所有的组合,比如给出绝对路径和通过configurationmanager访问,但都没有成功。

    下面的类在项目中,我们有web.config和appsettings。

    你能告诉我有什么问题吗。

    TestResources:AppDomainIsolatedTask
    {
           private NameValueCollection constants = (NameValueCollection)ConfigurationManager.GetSection("applicationSettings/applicationConstants");
    
           public override bool Execute()
           {
              constants.Get("VersionNo")
           }        
    
    }
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   miyamotogL    14 年前

    你试过这样的东西吗?

            var fileMap = new System.Configuration.ExeConfigurationFileMap { ExeConfigFilename = "path to config" };
            var config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            var section = (NameValueCollection)config.GetSection("name of section");