2012-05-11 48 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/