2012-05-11 49 views
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从配置

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

的ConfigFileLoc总是回来为空白。我想不通为什么它的空白.. 为什么字符串是空的 ..请帮助。

回答

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可能会帮助你

0

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

http://csd.codeplex.com/