关闭

企業類庫之Configuration-Migration Application Block(一)

584人阅读 评论(0) 收藏 举报
EditorFontData.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace Demo003
{
    
/// <summary>
    
/// 
    
/// </summary>

    public class EditorFontData:ConfigurationSection
    
{
        
/// <summary>
        
/// 
        
/// </summary>

        public EditorFontData()
        
{
        }


        
/// <summary>
        
/// 
        
/// </summary>

        ~EditorFontData()
        
{
        }


        
/// <summary>
        
/// 
        
/// </summary>

        [ConfigurationProperty("Name")]
        
public string Name
        
{
            
get
            
{
                
return Convert.ToString(this["Name"]);
            }

            
set
            
{
                
this["Name"= value;
            }

        }


        
/// <summary>
        
/// 
        
/// </summary>

        [ConfigurationProperty("Size")]
        
public double Size
        
{
            
get
            
{
                
return Convert.ToDouble(this["Size"]);
            }

            
set
            
{
                
this["Size"= value;
            }

        }


        
/// <summary>
        
/// 
        
/// </summary>

        [ConfigurationProperty("Style")]
        
public int Style
        
{
            
get
            
{
                
return Convert.ToInt32(this["Style"]);
            }

            
set
            
{
                
this["Style"= value;
            }

        }


        
/// <summary>
        
/// 
        
/// </summary>
        
/// <returns></returns>

        public override string ToString()
        
{
            StringBuilder sb 
= new StringBuilder();
            sb.AppendFormat(
"Name={0}; Size={1}; Style={2}", Name.ToString(), Size.ToString(), Style.ToString());
            
return sb.ToString();
        }

    }

}

 

App.config 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<configSections>
    
<section name="EditorSettings" type="Demo003.EditorFontData, Demo003, Version=1.1.0.0, Culture=neutral, PublicKeyToken=null" />
  
</configSections>
  
<EditorSettings Name="Verdana" Size="24" Style="2" />
</configuration>

 

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace Demo003
{
    
/// <summary>
    
/// 
    
/// </summary>

    public partial class Form1 : Form
    
{
        
/// <summary>
        
/// 
        
/// </summary>

        public Form1()
        
{
            InitializeComponent();
        }


        
/// <summary>
        
/// 
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void button1_Click(object sender, EventArgs e)
        
{
            
try
            
{
                EditorFontData aa 
= ConfigurationManager.GetSection("EditorSettings"as EditorFontData;
                MessageBox.Show(aa.ToString());
            }

            
catch (Exception Err)
            
{
                MessageBox.Show(Err.Message);
            }

        }


        
/// <summary>
        
/// 
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>

        private void button2_Click(object sender, EventArgs e)
        
{
            FontDialog fd 
= new FontDialog();
            EditorFontData aa 
= new EditorFontData();
            
if (fd.ShowDialog() == DialogResult.OK)
            
{
                aa.Name 
= fd.Font.Name;
                aa.Size 
= fd.Font.Size;
                aa.Style
=Convert.ToInt32(fd.Font.Style);

                Configuration config 
= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.Sections.Remove(
"EditorSettings");
                config.Sections.Add(
"EditorSettings", aa);
                config.Save();
            }

        }

    }

}
0
0

猜你在找
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【套餐】深度学习入门视频课程——唐宇迪
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】计算机视觉原理及实战——屈教授
【直播】机器学习之凸优化——马博士
【直播】机器学习&数据挖掘7周实训--韦玮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:221406次
    • 积分:3663
    • 等级:
    • 排名:第8820名
    • 原创:131篇
    • 转载:33篇
    • 译文:0篇
    • 评论:43条
    最新评论