winform 程序的配置文件——App.config

winform 程序的配置文件——App.config

Posted on 2005-09-26 17:11 sashow 阅读(668) 评论(3)   编辑  收藏 引用 网摘 所属分类: c# 编程

        在做 web 项目的时候,我一直在用 web.config ,觉得这个东西实在不错。偶然间发现,原来 winform 项目也有一个类似于 web.config 的文件——app.config,和呵,不胜欣喜!

一、添加app.config
        方法一:手工添加
         要添加这个文件很简单,执行如下操作就可以了:在开发环境中选择“项目”——>“添加新项”——>“XML 文件”,输入xml 文件的名称为 app.config。
        然后就可以如同 web.config 一样,编辑 app.config 文件,以及在应用程序中读、写该配置文件。
        重新编译项目,在应用程序的运行目录下会生成一个 app.config 的副本,名称为:程序名.exe.config。
        方法二:系统自动添加
        假设我们现在要使用 app.config 来动态配置一个 label 的 text 属性,那么,在label的属性窗口中选择如下图的属性进行配置,那么就可以让系统自动添加一个 app.config 的文件,呵呵,很省事的了。


二、编辑和使用app.config
        1、增加自定义的键值
        这个当然是增加<appSettings>标签了。下面给个例子:

<? xml version = " 1.0 "  encoding = " utf-8 " ?>  
< configuration >
    
< appSettings >
        
< add key = " port "  value = " 5450 " / >
        
< add key = " Thread "  value = " 20 " / >
    
</ appSettings >
< configuration >
        在这里我设置了一个叫做 port 和 Thread 的键,用来表示通讯端口和允许的最大线程数。
        根据应用的需要,我们可以添加许多需要动态配置的信息,如数据库连接字符串等。

        2、编辑系统原有的属性
        在下面的代码中,我配置了系统调试信息,这个东西挺不错的,可以将系统运行时输出的调试信息放置到一个文件。在我们使用 try ... catch 的时候,将 catch 到的信息输出给用户并不理想,因为用户看到一大堆的错误信息会感到很头痛,他们只希望知道操作成功还是没有成功。所以,将运行时的错误信息放置到文件是不错的选择。代码如下:
<? xml version = " 1.0 "  encoding = " utf-8 " ?>  
< configuration >
< system.diagnostics >
    
< switches >
        
< add name = " MagicTraceSwitch "  value = " 3 "   />
    
</ switches >
    
< trace autoflush = " true "  indentsize = " 4 " >
        
< listeners >
           
< add name = " myListener "  type = " System.Diagnostics.TextWriterTraceListener "  initializeData = " myListener.log "   />
           
< remove type = " System.Diagnostics.DefaultTraceListener "   />  
        
</ listeners >
    
</ trace >
</ system.diagnostics >
</ configuration >
测试代码如下:

 1 using  System;
 2 using  System.Drawing;
 3 using  System.Collections;
 4 using  System.ComponentModel;
 5 using  System.Windows.Forms;
 6 using  System.Data;
 7
 8 using  System.Diagnostics ;
 9 using  System.Configuration ;
10
11 namespace  AppTest
12 {
13    /// <summary>
14    /// Form1 的摘要说明。
15    /// </summary>

16    public class Form1 : System.Windows.Forms.Form
17    {
18        private System.Windows.Forms.Button button1;
19        /// <summary>
20        /// 必需的设计器变量。
21        /// </summary>

22        private System.ComponentModel.Container components = null;
23
24        public Form1()
25        {
26            //
27            // Windows 窗体设计器支持所必需的
28            //
29            Debug.WriteLine ("初始化 Form1");
30            InitializeComponent();
31
32            //
33            // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
34            //
35        }

36
37        /// <summary>
38        /// 清理所有正在使用的资源。
39        /// </summary>

40        protected override void Dispose( bool disposing )
41        {
42            Debug.WriteLine ("清理资源.");
43            if( disposing )
44            {
45                if (components != null
46                {
47                    components.Dispose();
48                }

49            }

50            base.Dispose( disposing );
51        }

52
53        Windows 窗体设计器生成的代码
83
84        /// <summary>
85        /// 应用程序的主入口点。
86        /// </summary>

87        [STAThread]
88        static void Main() 
89        {
90            Debug.WriteLine ("启动应用程序.");
91            Application.Run(new Form1());
92        }

93
94        private void button1_Click(object sender, System.EventArgs e) {
95            MessageBox.Show (ConfigurationSettings.AppSettings ["Port"].ToString ());
96        }

97    }

98}

99

 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值