【问题标题】:Can not get FileInfo back from configuration无法从配置中获取 FileInfo
【发布时间】:2012-05-11 12:34:07
【问题描述】:
<configuration>
  <configSections>
  <sectionGroup name="loggingConfigs">


    <section name="LoggingParameters"
          type="Framework.Logging.LoggingParameters, Framework.Logging" />

  </sectionGroup>
    </configSections>

  <loggingConfigs>

    <LoggingParameters

        myconfig="C:mydir\mypath1\mypath2\log4net.config" 

      />


  </loggingConfigs>
</configuration>   




public class LoggingParameters : ConfigurationSection
{

    [ConfigurationProperty("myconfig", IsRequired = true)]
    private string ConfigFileLoc
    {
        get { return (string)this["myconfig"]; }
    }


    public FileInfo MyConfigFile
    {
        get
        {
            string s = ConfigFileLoc;  <--- getting empty string here..don't know why
            return new FileInfo(s);
        }

    }
}

当我在我的应用程序的其他地方进行以下调用时,

FileInfo f = new Framework.Logging.LoggingParameters().MyConfigFile;

ConfigFileLoc 总是返回空白。我不知道为什么它是空白的。为什么字符串是空的..请帮忙。

【问题讨论】:

    标签: c# configuration app-config


    【解决方案1】:

    我不得不删除 SectionGroup(不知道这是否对您有要求)

      <configSections>    
          <section name="LoggingParameters"
                type="Framework.Logging.LoggingParameters, Framework.Logging" />      
      </configSections>
      <LoggingParameters myconfig="C:mydir\mypath1\mypath2\log4net.config" />
    

    并使用此代码获取 FileInfo(路径不再为空)

    LoggingParameters config = (LoggingParameters)ConfigurationSettings.GetConfig("LoggingParameters");
    FileInfo fInfo = config.MyConfigFile;
    

    This post by Scott Mitchell 可以帮到你

    【讨论】:

      【解决方案2】:

      我会避免手动编写配置访问代码。 foob​​ar 太容易了。查看这个插件,它可以让您直观地设计您的配置。让它为您编写样板代码,而您只专注于可视化设计器和您的问题域。

      http://csd.codeplex.com/

      【讨论】:

        猜你喜欢
        • 2019-04-28
        • 2019-05-19
        • 2020-06-26
        • 1970-01-01
        • 2020-05-26
        • 2016-10-08
        • 1970-01-01
        • 2022-11-08
        • 1970-01-01
        相关资源
        最近更新 更多