C# ConfigurationManager立即给出TypeInitializationException

C# ConfigurationManager立即给出TypeInitializationException,c#,xml,app-config,configurationmanager,C#,Xml,App Config,Configurationmanager,我正在制作一个需要读取其设置配置文件的应用程序 我在App_config文件中定义了如下设置: public sealed class SSWConfig : ConfigurationSection { private static SSWConfig settings = ConfigurationManager.GetSection("sswcomm") as SSWConfig; public static SSWConfig Settings

我正在制作一个需要读取其设置配置文件的应用程序

我在App_config文件中定义了如下设置:

public sealed class SSWConfig : ConfigurationSection
    {
        private static SSWConfig settings = ConfigurationManager.GetSection("sswcomm") as SSWConfig;

        public static SSWConfig Settings
        {
            get { return settings; }
        }

        [ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
        [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|\\", MinLength = 1, MaxLength = 256)]
        public string Id
        {
            get { return (string)this["id"]; }
            set { this["id"] = value; }
        }

        [ConfigurationProperty("connectionInterval", DefaultValue = "", IsKey = true, IsRequired = true)]
        public int ConnectionInterval
        {
            get { return (int)this["connectionInterval"]; }
            set { this["connectionInterval"] = value; }
        }

        [ConfigurationProperty("folders", IsDefaultCollection = true, IsKey = false, IsRequired = true)]
        public SSWConfigFolderCollection Folders
        {
            get { return base["folders"] as SSWConfigFolderCollection; }
        }

    }

    public sealed class SSWConfigFolderCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new SSWConfigFolderElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((SSWConfigFolderElement)element).Id;
        }

        new public SSWConfigFolderElement this[string Id]
        {
            get { return (SSWConfigFolderElement)base.BaseGet(Id); }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        public SSWConfigFolderElement this[int index]
        {
            get { return (SSWConfigFolderElement)base.BaseGet(index); }
        }

        protected override string ElementName
        {
            get
            {
                return "folder";
            }
        }
    }

    public sealed class SSWConfigFolderElement : ConfigurationElement
    {
        [ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
        [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|\\", MinLength = 1, MaxLength = 256)]
        public string Id
        {
            get { return (string)base["id"]; }
            set { base["id"] = value; }
        }

        [ConfigurationProperty("enable", DefaultValue = "true", IsKey = false, IsRequired = true)]
        public bool Enable
        {
            get { return (string)base["enable"] == "true"; }
            set { if (value) { base["enable"] = "true"; } else { base["enable"] = "false"; } }
        }

        [ConfigurationProperty("includeSubFolders", DefaultValue = "true", IsKey = false, IsRequired = true)]
        public bool IncludeSubFolders
        {
            get { return (string)base["includeSubFolders"] == "true"; }
            set { if (value) { base["includeSubFolders"] = "true"; } else { base["enable"] = "false"; } }
        }

        [ConfigurationProperty("fromPath", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string FromPath
        {
            get { return (string)base["fromPath"]; }
            set { base["fromPath"] = value; }
        }

        [ConfigurationProperty("wildcard", DefaultValue = "*.*", IsKey = false, IsRequired = true)]
        public string Wildcard
        {
            get { return (string)base["wildcard"]; }
            set { base["wildcard"] = value; }
        }

        [ConfigurationProperty("recipientId", DefaultValue = "", IsKey = false, IsRequired = true)]
        [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|\\", MinLength = 1, MaxLength = 256)]
        public string RecipientId
        {
            get { return (string)base["recipientId"]; }
            set { base["recipientId"] = value; }
        }
    }

C代码是这样的:

public sealed class SSWConfig : ConfigurationSection
    {
        private static SSWConfig settings = ConfigurationManager.GetSection("sswcomm") as SSWConfig;

        public static SSWConfig Settings
        {
            get { return settings; }
        }

        [ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
        [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|\\", MinLength = 1, MaxLength = 256)]
        public string Id
        {
            get { return (string)this["id"]; }
            set { this["id"] = value; }
        }

        [ConfigurationProperty("connectionInterval", DefaultValue = "", IsKey = true, IsRequired = true)]
        public int ConnectionInterval
        {
            get { return (int)this["connectionInterval"]; }
            set { this["connectionInterval"] = value; }
        }

        [ConfigurationProperty("folders", IsDefaultCollection = true, IsKey = false, IsRequired = true)]
        public SSWConfigFolderCollection Folders
        {
            get { return base["folders"] as SSWConfigFolderCollection; }
        }

    }

    public sealed class SSWConfigFolderCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new SSWConfigFolderElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((SSWConfigFolderElement)element).Id;
        }

        new public SSWConfigFolderElement this[string Id]
        {
            get { return (SSWConfigFolderElement)base.BaseGet(Id); }
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        public SSWConfigFolderElement this[int index]
        {
            get { return (SSWConfigFolderElement)base.BaseGet(index); }
        }

        protected override string ElementName
        {
            get
            {
                return "folder";
            }
        }
    }

    public sealed class SSWConfigFolderElement : ConfigurationElement
    {
        [ConfigurationProperty("id", DefaultValue = "", IsKey = true, IsRequired = true)]
        [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|\\", MinLength = 1, MaxLength = 256)]
        public string Id
        {
            get { return (string)base["id"]; }
            set { base["id"] = value; }
        }

        [ConfigurationProperty("enable", DefaultValue = "true", IsKey = false, IsRequired = true)]
        public bool Enable
        {
            get { return (string)base["enable"] == "true"; }
            set { if (value) { base["enable"] = "true"; } else { base["enable"] = "false"; } }
        }

        [ConfigurationProperty("includeSubFolders", DefaultValue = "true", IsKey = false, IsRequired = true)]
        public bool IncludeSubFolders
        {
            get { return (string)base["includeSubFolders"] == "true"; }
            set { if (value) { base["includeSubFolders"] = "true"; } else { base["enable"] = "false"; } }
        }

        [ConfigurationProperty("fromPath", DefaultValue = "", IsKey = false, IsRequired = true)]
        public string FromPath
        {
            get { return (string)base["fromPath"]; }
            set { base["fromPath"] = value; }
        }

        [ConfigurationProperty("wildcard", DefaultValue = "*.*", IsKey = false, IsRequired = true)]
        public string Wildcard
        {
            get { return (string)base["wildcard"]; }
            set { base["wildcard"] = value; }
        }

        [ConfigurationProperty("recipientId", DefaultValue = "", IsKey = false, IsRequired = true)]
        [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|\\", MinLength = 1, MaxLength = 256)]
        public string RecipientId
        {
            get { return (string)base["recipientId"]; }
            set { base["recipientId"] = value; }
        }
    }
现在,无论何时调用
SSWConfig
并将其放入变量中,我都会立即得到
TypeInitializationException
异常。我认为是因为
GetSection
部分返回
null
。这是因为我在App.config文件中犯了错误吗


带有嵌套元素的app.config对我来说是新的,所以可能是我定义错了。

刚刚发布了我的解决方案

显然,我让事情变得比实际情况更难。我从零开始,经过一些修改后,我的设置被读取

我遇到的另一个错误是,我从未真正读取过配置文件(oops),认为调用配置类时会自动读取配置文件

类别:

//设置
公共类SSWConfigSSWComm:ConfigurationSection
{
[配置属性(“id”)]
公共字符串Id
{
获取{返回(字符串)this[“id”];}
设置{this[“id”]=value;}
}
[ConfigurationProperty(“connectionInterval”)]
公共int连接区间
{
获取{return(int)this[“connectionInterval”];}
设置{this[“connectionInterval”]=value;}
}
[配置属性(“toPath”)]
公共字符串ToPath
{
获取{return(string)this[“toPath”];}
设置{this[“toPath”]=value;}
}
[配置属性(“文件夹”)]
[ConfigurationCollection(typeof(SSWConfigFolderElement),AddItemName=“folder”)]
公共SSWConfigFolderElementCollection sswConfigFolders
{
获取{return(SSWConfigFolderElementCollection)基[“文件夹”];}
}
}
//背景
[配置集合(类型(SSWConfigFolderElement))]
公共类SSWConfigFolderElementCollection:ConfigurationElementCollection
{
新的公共SSWConfigFolderElement此[字符串名称]
{
获取{return(SSWConfigFolderElement)base.BaseGet(name);}
}
公共SSWConfigFolderElement此[int索引]
{
获取{return(SSWConfigFolderElement)base.BaseGet(index);}
}
受保护的覆盖ConfigurationElement CreateNewElement()
{
返回新的SSWConfigFolderElement();
}
受保护的覆盖对象GetElementKey(ConfigurationElement元素)
{
返回((SSWConfigFolderElement)元素).Id;
}
}
公共类SSWConfigFolderElement:ConfigurationElement
{
[配置属性(“id”)]
公共字符串Id
{
获取{return(string)base[“id”];}
设置{base[“id”]=value;}
}
[配置属性(“启用”)]
公共布尔启用
{
获取{return(string)base[“enable”]=“true”;}
设置{if(value){base[“enable”]=“true”;}否则{base[“enable”]=“false”;}
}
[ConfigurationProperty(“includeSubFolders”)]
公共bool包括子文件夹
{
获取{return(string)base[“includeSubFolders”]==“true”;}
设置{if(value){base[“includeSubFolders”]=“true”}否则{base[“enable”]=“false”;}
}
[ConfigurationProperty(“fromPath”)]
公共字符串FromPath
{
获取{return(string)base[“fromPath”];}
设置{base[“fromPath”]=value;}
}
[配置属性(“通配符”)]
公共字符串通配符
{
获取{return(string)base[“wildcard”];}
集合{base[“通配符”]=value;}
}
[ConfigurationProperty(“recipientId”)]
[StringValidator(InvalidCharacters=“~!@$%^&*()[]{}/;”\“\”,MinLength=1,MaxLength=256)]
公共字符串RecipientId
{
获取{return(string)base[“recipientId”];}
设置{base[“recipientId”]=value;}
}
}
收集数据

Configuration conf=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
SSWConfigSSWComm sc=(SSWConfigSSWComm)conf.GetSection(“sswcomm”);
字符串Id=sc.Id;
long lngInterval=sc.ConnectionInterval;
字符串toPath=sc.toPath;
SSWConfigFolderElementCollection fec=sc.sswConfigFolders;
foreach(fec中的SSWConfigFolderElement fe)
{
字符串fromPath=fe.fromPath;
字符串通配符=fe.通配符;
bool includeSubFolders=fe.includeSubFolders;
字符串recipientId=fe.recipientId;
sp.GetFiles(Id、recipientId、fromPath、toPath、通配符,包括子文件夹);
}

希望这会对将来的人有所帮助。

只是发布我的解决方案

很明显,我让它变得比实际更难。我从零开始,经过一些修改,我的设置被读取

我遇到的另一个错误是,我从未真正读取过配置文件(oops),认为调用配置类时会自动读取配置文件

类别:

//设置
公共类SSWConfigSSWComm:ConfigurationSection
{
[配置属性(“id”)]
公共字符串Id
{
获取{返回(字符串)this[“id”];}
设置{this[“id”]=value;}
}
[ConfigurationProperty(“connectionInterval”)]
公共int连接区间
{
获取{return(int)this[“connectionInterval”];}
设置{this[“connectionInterval”]=value;}
}
[配置属性(“toPath”)]
公共字符串ToPath
{
获取{return(string)this[“toPath”];}
设置{this[“toPath”]=value;}
}
[配置属性(“文件夹”)]
[ConfigurationCollection(typeof(SSWConfigFolderElement),AddItemName=“folder”)]
公共SSWConfigFolderElementCollection sswConfigFolders
{
获取{return(SSWConfigFolderElementCollection)基[“文件夹”];}
}
}
//背景
[确认]