首页 新闻 会员 周边 捐助

在练习建造者模式时,出现错误:System.Configuration.ConfigurationErrorsException,如何解决?

0
悬赏园豆:5 [已解决问题] 解决于 2020-08-10 11:24

代码:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;


namespace Builder
{
    class Program
    {
        static void Main(string[] args)
        {
            FoodManager foodManager = new FoodManager();

            Builder instance;

            Console.WriteLine("please enter food no:");

            string No = Console.ReadLine();

            string foodType = ConfigurationManager.AppSettings["No"+No];

            instance = (Builder)Assembly.Load("Builder").CreateInstance("Builder." + foodType);

            foodManager.Construct(instance);

            Food food = instance.GetFood();

            food.Show();

            Console.ReadLine();
        }
    }
}

错误信息:
System.Configuration.ConfigurationErrorsException
HResult=0x80131902
Message=配置系统未能初始化
Source=System.Configuration
StackTrace:
at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName)
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
at System.Configuration.ConfigurationManager.get_AppSettings()
at Builder.Program.Main(String[] args) in D:\work\WebApplication1\Builder\Program.cs:line 23

Inner Exception 1:
ConfigurationErrorsException: 无法识别的配置节 AppSettings。 (D:\work\WebApplication1\Builder\bin\Debug\Builder.exe.Config line 6)

我想问一下这个错误怎么解决呢?

c#
小顽童00的主页 小顽童00 | 初学一级 | 园豆:199
提问于:2020-07-05 11:46
< >
分享
最佳答案
1

看起来是配置的文件有问题,你配置文件什么写的

收获园豆:5
会长 | 专家六级 |园豆:12463 | 2020-07-06 10:52
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
    </startup>
  <AppSettings>
    <clear />
    <add key="No1" value="GoldBuider"></add>
    <add key="No2" value="NomalBuider"></add>
  </AppSettings>
</configuration>

会长,麻烦帮忙看一下~

小顽童00 | 园豆:199 (初学一级) | 2020-07-31 21:33

@StepStep: <AppSettings>应为<appSettings>,别忘了结贴给我园豆

会长 | 园豆:12463 (专家六级) | 2020-08-03 08:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册