我的app.config文件有什么问题?

时间:2021-11-06 07:28:36

I have an app.config file that looks like this:

我有一个app.config文件,如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="TestKey" value="TestValue" />
  </appSettings>
  <newSection>
  </newSection>
</configuration>

And I'm trying to use it in this way:

而我正试图以这种方式使用它:

System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(@"C:\app.config");  
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap); 

However, it doesn't seem to be working. When I break and debug right after the file is read in, and I try to look at configuration.AppSettings I get an 'configuration.AppSettings' threw an exception of type 'System.InvalidCastException'.

但是,它似乎没有起作用。当我在读入文件后立即中断和调试时,我尝试查看configuration.AppSettings我得到'configuration.AppSettings'引发类型'System.InvalidCastException'的异常。

I'm sure I'm reading the file, because when I look at configuration.Sections["newSection"] I am returned an empty {System.Configuration.DefaultSection} (rather than null).

我确定我正在读取该文件,因为当我查看configuration.Sections [“newSection”]时,我返回一个空的{System.Configuration.DefaultSection}(而不是null)。

I'm guessing I've got something very basic wrong...what's going on with AppSettings?

我猜我有一些非常基本的错误...... AppSettings会发生什么?

4 个解决方案

#1


13  

You are using a wrong function to read the app.config. OpenMappedMachineConfiguration is intended to open your machine.config file, but you are opening a typical application.exe.config file. The following code will read your app.config and return what you'd expect.

您正在使用错误的函数来读取app.config。 OpenMappedMachineConfiguration用于打开machine.config文件,但您打开的是典型的application.exe.config文件。以下代码将读取您的app.config并返回您期望的内容。

    System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
    fileMap.ExeConfigFilename = @"C:\app.config";
    System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
    MessageBox.Show(configuration.AppSettings.Settings["TestKey"].Value);

#2


3  

I think the 'newSection' element is causing the problem. Unless you add a 'configSections' element too, to declare what 'newSection' is, .NET won't be able to cast it.

我认为'newSection'元素导致问题。除非你添加'configSections'元素,要声明'newSection'是什么,.NET将无法强制转换它。

You need something like:

你需要这样的东西:

<configSections>
  <section name="newSection" type="Fully.Qualified.TypeName.NewSection,   
  AssemblyName" />
</configSections>

In the first instance, I'd try removing the 'newSection' element to see if this improves the situation.

在第一个例子中,我尝试删除'newSection'元素,看看这是否改善了这种情况。

This link explains about Custom Configuration Sections.

此链接说明了自定义配置部分。

#3


3  

If you read the documentation on MSDN on the function you try to use:

如果您在尝试使用的功能上阅读MSDN上的文档:

OpenExeConfiguration MSDN

In the way you are using it will try to find the configuration of app.config.exe. If you do want to make use of appSettings, add them to the configuration of the config file from your application and then access them by using the configuration manager:

在您使用它的方式将尝试找到app.config.exe的配置。如果您确实想要使用appSettings,请将它们添加到应用程序的配置文件配置中,然后使用配置管理器访问它们:

Using appsetting .net MSDN

使用appsetting .net MSDN

#4


2  

Any time i've used a key in my webconfig i've done it like so

任何时候我在我的webconfig中使用了一个键,我就这样做了

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <SectionGroup>
      Section Stuff
    </SectionGroup>
  </configSections>
<appsettings>
   <add key="TestKey" value="TestValue" />
</appSettings>
</configuration>

I don't fully understand why but it would always throw errors at me having app settings inside of configsettings.

我不完全理解为什么,但它总是会让我在configsettings中有应用程序设置时出错。

#1


13  

You are using a wrong function to read the app.config. OpenMappedMachineConfiguration is intended to open your machine.config file, but you are opening a typical application.exe.config file. The following code will read your app.config and return what you'd expect.

您正在使用错误的函数来读取app.config。 OpenMappedMachineConfiguration用于打开machine.config文件,但您打开的是典型的application.exe.config文件。以下代码将读取您的app.config并返回您期望的内容。

    System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
    fileMap.ExeConfigFilename = @"C:\app.config";
    System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
    MessageBox.Show(configuration.AppSettings.Settings["TestKey"].Value);

#2


3  

I think the 'newSection' element is causing the problem. Unless you add a 'configSections' element too, to declare what 'newSection' is, .NET won't be able to cast it.

我认为'newSection'元素导致问题。除非你添加'configSections'元素,要声明'newSection'是什么,.NET将无法强制转换它。

You need something like:

你需要这样的东西:

<configSections>
  <section name="newSection" type="Fully.Qualified.TypeName.NewSection,   
  AssemblyName" />
</configSections>

In the first instance, I'd try removing the 'newSection' element to see if this improves the situation.

在第一个例子中,我尝试删除'newSection'元素,看看这是否改善了这种情况。

This link explains about Custom Configuration Sections.

此链接说明了自定义配置部分。

#3


3  

If you read the documentation on MSDN on the function you try to use:

如果您在尝试使用的功能上阅读MSDN上的文档:

OpenExeConfiguration MSDN

In the way you are using it will try to find the configuration of app.config.exe. If you do want to make use of appSettings, add them to the configuration of the config file from your application and then access them by using the configuration manager:

在您使用它的方式将尝试找到app.config.exe的配置。如果您确实想要使用appSettings,请将它们添加到应用程序的配置文件配置中,然后使用配置管理器访问它们:

Using appsetting .net MSDN

使用appsetting .net MSDN

#4


2  

Any time i've used a key in my webconfig i've done it like so

任何时候我在我的webconfig中使用了一个键,我就这样做了

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <SectionGroup>
      Section Stuff
    </SectionGroup>
  </configSections>
<appsettings>
   <add key="TestKey" value="TestValue" />
</appSettings>
</configuration>

I don't fully understand why but it would always throw errors at me having app settings inside of configsettings.

我不完全理解为什么,但它总是会让我在configsettings中有应用程序设置时出错。