C# 自定义配置未读取

奥雷尔·邦万

我正在用 C# 开发程序,并且第一次使用自定义配置。

我的程序正常启动,但配置文件似乎没有被读取。

这是我的“app.config”配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="Files" type="Initialisation.FilesSection, Initialisation" >
      <section name="Source" type="Initialisation.SourceElement, Initialisation" />

      <section name="List" type="Initialisation.ListElementCollection, Initialisation" />
      <section name="File" type="Initialisation.FileElement, Initialisation" />
    </sectionGroup>
  </configSections>

  <Files>
    <Source path="\\192.168.21.6\tmp\" />

    <List>
      <File name="Adobe Reader"       file="readerdc_fr_xa_crd_install.exe"           default="true"  />
      <File name="Adobe Flash"        file="flashplayer27pp_xa_install.exe"           default="false" />
      <File name="Java"               file="java.exe"                                 default="true"  />
    </List>
  </Files>
</configuration>

这是管理文件的类

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Initialisation
{
    public class Files
    {
        //private static FilesSection _config;

        private Files()
        {
        }

        public static FilesSection Config
        {
            get
            {
                /*if (_config == null)
                {
                    _config = global::System.Configuration.ConfigurationManager.GetSection("Files") as FilesSection;
                }
                return _config;*/
                return ((FilesSection)(ConfigurationManager.GetSection("Files")));
            }
        }
    }

    public class FilesSection : System.Configuration.ConfigurationSection
    {

        [System.Configuration.ConfigurationPropertyAttribute("Source")]
        public SourceElement Source
        {
            get
            {
                return ((SourceElement)(this["Source"]));
            }
        }

        [System.Configuration.ConfigurationPropertyAttribute("List")]
        [System.Configuration.ConfigurationCollectionAttribute(typeof(FileElement), AddItemName = "File")]
        public ListElementCollection List
        {
            get
            {
                return ((ListElementCollection)(this["List"]));
            }
        }
    }

    public class SourceElement : System.Configuration.ConfigurationElement
    {

        [System.Configuration.ConfigurationPropertyAttribute("path", IsRequired = true)]
        public string Path
        {
            get
            {
                return ((string)(this["path"]));
            }
            set
            {
                this["path"] = value;
            }
        }
    }

    public class ListElementCollection : System.Configuration.ConfigurationElementCollection
    {

        public FileElement this[int i]
        {
            get
            {
                return ((FileElement)(this.BaseGet(i)));
            }
        }

        protected override System.Configuration.ConfigurationElement CreateNewElement()
        {
            return new FileElement();
        }

        protected override object GetElementKey(System.Configuration.ConfigurationElement element)
        {
            return ((FileElement)(element)).Name;
        }
    }

    public class FileElement : System.Configuration.ConfigurationElement
    {

        [System.Configuration.ConfigurationPropertyAttribute("name", IsRequired = true)]
        public string Name
        {
            get
            {
                return ((string)(this["name"]));
            }
            set
            {
                this["name"] = value;
            }
        }

        [System.Configuration.ConfigurationPropertyAttribute("file", IsRequired = true)]
        public string File
        {
            get
            {
                return ((string)(this["file"]));
            }
            set
            {
                this["file"] = value;
            }
        }

        [System.Configuration.ConfigurationPropertyAttribute("default", IsRequired = false, DefaultValue = false)]
        public bool Default
        {
            get
            {
                return ((bool)(this["default"]));
            }
            set
            {
                this["default"] = value;
            }
        }
    }
}

我正在尝试获得这样的值:

FilesSection fs = Files.Config;
SourceElement se = fs.Source;
string path = se.Path;

MessageBox.Show(path);

但是所有变量(fs、se 和 path)都为空。

我阅读了很多帖子,并且已经尝试了很多解决方案:/

加布里埃尔·奎因

问题是Files类是一个ConfigurationSection,但您在 app.config上将它声明为sectionGroup其他元素也是如此,它们是ConfigurationElement但被声明为section元素。

configSections 中删除所有ConfigurationElements,因为它们不需要声明,并将 Files 声明从sectionGroup 更改section

您的configSections应如下所示:

<configSections>
    <section name="Files" type="Initialisation.FilesSection, Initialisation" />
</configSections>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C#读取自定义配置版本

来自分类Dev

从C#读取protobuf3自定义选项

来自分类Dev

QT C ++读取自定义文件结构

来自分类Dev

C#自定义配置版本控制

来自分类Dev

C#自定义配置版本控制

来自分类Dev

C#自定义操作未删除特定文件

来自分类Dev

C#WebBrowser控件未显示自定义HTML

来自分类Dev

Visual C#在Appdata中的自定义XML中存储和读取自定义选项

来自分类Dev

Visual C#在Appdata中的自定义XML中存储和读取自定义选项

来自分类Dev

C#-从自定义应用程序读取嵌套的事件日志

来自分类Dev

如何在C ++中读取自定义格式的文本文件

来自分类Dev

使用Google Analytics API C#读取自定义变量的数据

来自分类Dev

调用函数时,c ++无法使用自定义类读取内存

来自分类Dev

如何在c中使用自定义类型从节点列表中读取数据结构

来自分类Dev

C#- 自定义控件的自定义属性

来自分类Dev

使用配置元素集合c#实现自定义部分

来自分类Dev

在程序配置中使用C#NLog自定义定界符

来自分类Dev

使用配置元素集合c#实现自定义部分

来自分类Dev

如何从C ++中的配置文件加载自定义“ const”变量?

来自分类Dev

Sonarcloud + Travis 无法自定义 C 质量配置文件

来自分类Dev

应用自定义窗口样式和模板(C#,WPF)后未显示验证错误

来自分类Dev

子类未调用Objective-C基类属性自定义Getter

来自分类Dev

具有自定义设计代码的C#表单未显示

来自分类Dev

子类未调用Objective-C基类属性自定义Getter

来自分类Dev

在自定义UITableViewCell中未显示Objective-C图像

来自分类Dev

DocuSign - C# SDK 提供的“EnvelopesApi”的“ListDocuments”API 未返回“自定义文档字段”

来自分类Dev

在B2C自定义配置文件中,我无法获得包含用户组的自定义声明

来自分类Dev

目标C自定义比较:优先

来自分类Dev

C ++自定义容器传递{}列表

Related 相关文章

  1. 1

    C#读取自定义配置版本

  2. 2

    从C#读取protobuf3自定义选项

  3. 3

    QT C ++读取自定义文件结构

  4. 4

    C#自定义配置版本控制

  5. 5

    C#自定义配置版本控制

  6. 6

    C#自定义操作未删除特定文件

  7. 7

    C#WebBrowser控件未显示自定义HTML

  8. 8

    Visual C#在Appdata中的自定义XML中存储和读取自定义选项

  9. 9

    Visual C#在Appdata中的自定义XML中存储和读取自定义选项

  10. 10

    C#-从自定义应用程序读取嵌套的事件日志

  11. 11

    如何在C ++中读取自定义格式的文本文件

  12. 12

    使用Google Analytics API C#读取自定义变量的数据

  13. 13

    调用函数时,c ++无法使用自定义类读取内存

  14. 14

    如何在c中使用自定义类型从节点列表中读取数据结构

  15. 15

    C#- 自定义控件的自定义属性

  16. 16

    使用配置元素集合c#实现自定义部分

  17. 17

    在程序配置中使用C#NLog自定义定界符

  18. 18

    使用配置元素集合c#实现自定义部分

  19. 19

    如何从C ++中的配置文件加载自定义“ const”变量?

  20. 20

    Sonarcloud + Travis 无法自定义 C 质量配置文件

  21. 21

    应用自定义窗口样式和模板(C#,WPF)后未显示验证错误

  22. 22

    子类未调用Objective-C基类属性自定义Getter

  23. 23

    具有自定义设计代码的C#表单未显示

  24. 24

    子类未调用Objective-C基类属性自定义Getter

  25. 25

    在自定义UITableViewCell中未显示Objective-C图像

  26. 26

    DocuSign - C# SDK 提供的“EnvelopesApi”的“ListDocuments”API 未返回“自定义文档字段”

  27. 27

    在B2C自定义配置文件中,我无法获得包含用户组的自定义声明

  28. 28

    目标C自定义比较:优先

  29. 29

    C ++自定义容器传递{}列表

热门标签

归档