无法从配置中取回文件信息

本文关键字:回文 文件 信息 配置 | 更新日期: 2023-09-27 18:33:45

<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;

配置文件位置始终返回为空白。我不知道为什么它是空白的。为什么字符串为空..请帮忙。

无法从配置中取回文件信息

我不得不删除SectionGroup(不知道这是否是你的要求)

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

并用这个代码得到了文件信息(路径不再为空)

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

斯科特·米切尔的这篇文章可能会帮助你

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

http://csd.codeplex.com/