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

本文关键字:配置文件 读取 应用程序 控制台 | 更新日期: 2023-09-27 18:09:01

我试图在Visual Studio 2012的项目中从app.config文件中读取两个变量。然而,我得到一个异常:"配置系统初始化失败"。有什么问题吗?

这是我的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);
            }
        }
}

似乎不能只初始化…当我转储异常时,异常显示:Configuration System failed to initialize。有什么建议吗

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

再次检查代码:

  <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" />type属性应提供类的完整汇编限定名。也就是说,如果你的程序集被称为ConnectionManager,你应该配置ConnectionManager.MyConnectionConfigurationSection, ConnectionManager

顺便说一句,在我看来,如果你要配置2个简单的设置,我不会使用自定义配置部分(这是多余的)。为什么不直接使用。net配置模型中内置的appSettings呢?

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

你可以这样访问这些参数:

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

检查您的配置文件:

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