0
<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 总是返回空白。我不知道为什么它是空白的..为什么字符串是空的..请帮忙。

4

2 回答 2

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;

Scott Mitchell 的这篇文章可能会对您有所帮助

于 2012-05-11T04:22:38.633 回答
0

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

http://csd.codeplex.com/

于 2012-05-11T04:34:07.807 回答