2014-06-18 55 views
0

我想從我的項目在Visual Studio 2012中的app.config文件中讀取兩個變量。但是我得到一個異常:「配置系統無法初始化」。任何想法這裏有什麼不對?無法讀取控制檯應用程序中的配置文件在c#

這裏是我的app.config文件

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <configurationSections> 
    <section name ="connectionConfig" type="ConnectionManager.MyConnectionConfigurationSection" 
      /> 
    </configurationSections> 
    <connectionConfig portNumber="7777"/> 
    <connectionConfig hostName="localhost" /> 
</configuration> 

C#文件:

  namespace ConnectionManager 
      { 
       public class MyConnectionConfigurationSection : System.Configuration.ConfigurationSection 
       { 
        [ConfigurationProperty("portNumber")] 
        public string PortNumber 
        { 
         get 
         { 
          return (string)this["portNumber"]; 
         } 
        set 
        { 
         this["portNumber"] = value; 
        } 
       } 

       [ConfigurationProperty("hostName")] 
       public string HostName 
       { 
        get 
        { 
         return (string)this["hostName"]; 
        } 
        set 
        { 
         this["hostName"] = value; 
        } 
       } 
      } 

      public static class ConnectionApplication 
      { 
       public static MyConnectionConfigurationSection Config { get; internal set; } 

       public static void Initialize() 
       { 
        try 
        { 
         Config = ConfigurationManager.GetSection("connectionConfig") as MyConnectionConfigurationSection ; 
        } 
        catch (Exception ex) 
        { 
         Console.WriteLine(ex.Message.ToString()); 
        } 

       } 

      } 
    class Program 
     { 
      static string portNumber; 
      static string hostName; 
      static void Main(string[] args) 
      { 
       ConnectionApplication.Initialize(); 

       portNumber=ConnectionApplication.Config.PortNumber; 
       Console.WriteLine(portNumber); 

       hostName = ConnectionApplication.Config.HostName; 
       Console.WriteLine(hostName); 

      } 
     } 
} 

似乎它不能夠只初始化......當我轉儲異常,異常說:配置系統無法初始化。任何建議我在做什麼錯

+0

您已經聲明'PortNumber'屬性'portNubmer'聲明爲'int' 。 – Hassan

+0

對不起,這是一個錯字...現在糾正 – Why

回答

3

再次檢查你的代碼:

<configurationSections> 
    <section name ="connectionConfig" type="ConnectionManager.MyConnectionConfigurationSection" 
      /> 
    </configurationSections> 
    <connectionConfig portNumber="7777"/> 
    <connectionConfig hostName="localhost" /> 
  • 這是<configSections>
  • 你爲什麼兩次聲明自定義欄目? <connectionConfig portNumber="7777" hostName="localhost" />是使用您的自定義部分的正確方法。
  • <section name ="connectionConfig" type="ConnectionManager.MyConnectionConfigurationSection" /> on type屬性應提供該類的完整裝配限定名稱。也就是說,如果您的程序集名爲ConnectionManager,則應該配置ConnectionManager.MyConnectionConfigurationSection, ConnectionManager

順便說一句,在我看來,如果你要配置2個簡單的設置,我不會使用自定義配置節(這是一個矯枉過正)。你爲什麼不使用內建在.NET配置模型上的appSettings

<appSettings> 
    <add key="connectionManager:host" value="localhost" /> 
    <add key="connectionManager:port" value="7777" /> 
</appSettings> 

你會訪問這些參數是這樣的:作爲`string`以及`Program`類

string host ConfigurationManager.AppSettings["connectionManager:host"]; 
int port = int.Parse(ConfigurationManager.AppSettings["connectionManager:port"]); 
+0

實際上,我試圖瞭解自定義配置,因此只是在玩...謝謝你的建議 – Why

+0

@爲什麼我很高興這解決了你的問題。自.NET早期開始,我一直使用自定義配置部分,這是一種非常強大的方法,因爲您獲得了一個類型化的配置模型,並且不需要浪費時間使用自己的配置驗證引擎...... –

+0

非常感謝爲您提供幫助 – Why

2

檢查您的配置文件:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <configurationSections> 
     <section name ="connectionConfig" type="ConnectionManager.MyConnectionConfigurationSection"/> 
    </configurationSections> 
    <connectionConfig portNumber="7777"/> 
    <connectionConfig hostName="localhost" /> 
</configuration> 

包含端口號和主機名的部分是部分以外的代碼你的閱讀。

試試這樣說:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> 
    </startup> 
    <configurationSections> 
     <section name ="connectionConfig" type="ConnectionManager.MyConnectionConfigurationSection">   
      <connectionConfig portNumber="7777"/> 
      <connectionConfig hostName="localhost" /> 
     </section> 
    </configurationSections> 
</configuration> 
+0

你確定你提供了一個工作解決方案嗎? –

+1

不,我提供了一個想法,可以解決問題。 – CloudyMarble

+0

我相信你的代碼不起作用。也許我完全錯了,但這不是.NET配置模式 –

相關問題