简体   繁体   English

无法在C#的控制台应用程序中读取配置文件

[英]Unable to read from config file in console app in c#

I am trying to read two variables from app.config file in my project in Visual Studio 2012. However I get an exception of : "Configuration System failed to initialize". 我正在尝试从Visual Studio 2012中的项目中的app.config文件读取两个变量。但是,我得到一个例外:“配置系统初始化失败”。 Any ideas what is wrong here? 有什么主意在这里吗?

Here is my app.config file 这是我的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>

The C# file: 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);

            }
        }
}

It seems that its not being able to initialize only...when i dump the exception, the exception says : Configuration System failed to initialize. 似乎它不能仅初始化...当我转储异常时,异常显示:配置系统无法初始化。 Any suggestions what am i doing wrong 任何建议我在做什么错

Check your code again: 再次检查您的代码:

  <configurationSections>
    <section name ="connectionConfig" type="ConnectionManager.MyConnectionConfigurationSection"
             />
  </configurationSections>
  <connectionConfig  portNumber="7777"/>
  <connectionConfig  hostName="localhost" />
  • It's <configSections> 这是<configSections>
  • Why do you declare twice your custom section? 为什么要对自定义部分声明两次? <connectionConfig portNumber="7777" hostName="localhost" /> is the right way of using your custom section. <connectionConfig portNumber="7777" hostName="localhost" />是使用自定义部分的正确方法。
  • <section name ="connectionConfig" type="ConnectionManager.MyConnectionConfigurationSection" /> on type attribute should provide the full assembly qualified name of the class. type属性上的<section name ="connectionConfig" type="ConnectionManager.MyConnectionConfigurationSection" />应该提供该类的完整程序集限定名称。 That is, if your assembly is called ConnectionManager you should configure ConnectionManager.MyConnectionConfigurationSection, ConnectionManager . 也就是说,如果您的程序集名为ConnectionManager ,则应该配置ConnectionManager.MyConnectionConfigurationSection, ConnectionManager

BTW, in my opinion, if you're going to configure 2 simple settings, I wouldn't use a custom configuration section (this is an overkill). 顺便说一句,在我看来,如果您要配置2个简单的设置,那么我不会使用自定义配置部分(这是一个过大的选择)。 Why don't you just use appSettings which is built-in on .NET configuration model? 您为什么不只使用.NET配置模型中内置的appSettings

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

And you'll access these parameters this way: 您将通过以下方式访问这些参数:

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

Check your Configuration file: 检查您的配置文件:

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

The part containing the portnumber and hostname is outside the section your reading in your code. 包含端口号和主机名的部分不在您的代码阅读范围内。

Try it like this: 像这样尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM