winform 程序的配置文件——App.config
来源:互联网 发布:绿标网址域名防红跳转 编辑:程序博客网 时间:2024/06/05 18:04
在做 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>测试代码如下:
1using System;
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7
8using System.Diagnostics ;
9using System.Configuration ;
10
11namespace 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 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
54 /**//// <summary>
55 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
56 /// 此方法的内容。
57 /// </summary>
58 private void InitializeComponent()
59 {
60 System.Configuration.AppSettingsReader configurationAppSettings = new System.Configuration.AppSettingsReader();
61 this.button1 = new System.Windows.Forms.Button();
62 this.SuspendLayout();
63 //
64 // button1
65 //
66 this.button1.Location = new System.Drawing.Point(128, 128);
67 this.button1.Name = "button1";
68 this.button1.TabIndex = 0;
69 this.button1.Text = "button1";
70 this.button1.Click += new System.EventHandler(this.button1_Click);
71 //
72 // Form1
73 //
74 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
75 this.ClientSize = new System.Drawing.Size(292, 266);
76 this.Controls.Add(this.button1);
77 this.Name = "Form1";
78 this.Text = ((string)(configurationAppSettings.GetValue("Form1.Text", typeof(string))));
79 this.ResumeLayout(false);
80
81 }
82 #endregion
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
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/long2006sky/archive/2007/08/11/1737738.aspx
- winform 程序的配置文件——App.config
- winform 程序的配置文件——App.config
- C# WinForm程序的App.Config数据库连接配置文件
- C# WinForm程序App.Config数据库连接配置文件的使用过程
- C#WinForm APP.CONFIG配置文件的操作
- WinForm程序Config配置文件的一点知识
- 有关winform 配置文件 app.config
- WinForm中的配置文件app.Config
- winform读取配置文件App.config
- winform程序读取和改写配置文件App.config元素的值
- winform程序读取和改写配置文件App.config元素的值
- winform程序读取和改写配置文件App.config元素的值
- c#读取配置文件 程序的App.config
- .NET开发使用Winform的配置文件App.config
- C# winform 修改app.config配置文件的实现类
- winform的app.config
- c#,WinForm中读写配置文件App.config
- winform读取app.config.配置文件
- .Net中的文件监视功能实现
- 事件处理类
- ubuntu默认root密码
- ::DialogBoxParam (hInstance, MAKEINTRESOURCE (IDD_WININFO), NULL, MainDlgProc, NULL);
- 解决Linux iptables防火墙和vsftpd的问题
- winform 程序的配置文件——App.config
- c语言 宏
- Head First C# 中文版 第12章 回顾与前瞻 page550
- Java 按行读取文本文件内容
- oracle 分析语句
- fedora10教育网能用的更新源
- windows oracle bat 启动
- 不在body标签上加载onload事件,实现这种效果
- SetCursor