本文介绍了在配置文件中创建自定义部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在app.config文件中创建自定义部分

I am trying to create custom section in app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="BlogSettings" type="ConsoleApplication1.BlogSettings,
      ConsoleApplication1" />
  </configSections>
  <BlogSettings
    Price="10"
    title="BLACKswastik" />
</configuration>

C#代码:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string title = BlogSettings.Settings.Title;

            Console.WriteLine(title);
            Console.ReadKey();
        }
    }

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

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

        [ConfigurationProperty("Price"
          , DefaultValue = 20
          , IsRequired = false)]
        [IntegerValidator(MinValue = 1
          , MaxValue = 100)]
        public int Price
        {
            get { return (int)this["Price"]; }
            set { this["Price"] = value; }
        }


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

但是当我运行这段代码时,我得到了这个错误:

but when I run this code I am getting this error:

请告诉我我做错了什么事.

Please suggest me whats wrong I am doing.

推荐答案

您应该在CodeProject上查看Jon Rista关于.NET 2.0配置的三部分系列文章.

You should check out Jon Rista's three-part series on .NET 2.0 configuration up on CodeProject.

  • Unraveling the mysteries of .NET 2.0 configuration
  • Decoding the mysteries of .NET 2.0 configuration
  • Cracking the mysteries of .NET 2.0 configuration

强烈推荐,写得很好,非常有帮助!这将使您对.NET配置系统有透彻的了解.

Highly recommended, well written and extremely helpful! This will give you a thorough understanding of the .NET configuration system.

Phil Haack也有一篇很棒的博客文章自定义配置部分的三个简单步骤,可让您快速开始构建自己的自定义配置部分.

Phil Haack also has a great blog post Three easy steps to a custom configuration section that will give you a quick head-start into building your own custom config sections.

要设计自己的自定义部分,还有一个方便的工具(Visual Studio外接程序),名为配置部分设计器这将使创建自己的自定义部分变得非常容易和简单,并使它建立所有必要的代码来处理该自定义部分.

To design your own custom section, there's also a handy tool (Visual Studio add-in) called Configuration Section Designer that will make it very easy and simple to create your own custom section and have it build up all the necessary code to handle that custom section.

这篇关于在配置文件中创建自定义部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 02:55