C 对App.config文件读取及写入方法

C 对App.config文件读取及写入方法在 C 项目里 常常会把部分常用的参数值写入到 App config 文件之中 当程序启动并开始运行时 会率先读取上一次写入的数值 本文将以模拟量对应通道的设置值以及最大值为例 来实现对应内容的读取与写入操作 1 双击打开 App

大家好,欢迎来到IT知识分享网。

在 C# 项目里,常常会把部分常用的参数值写入到 App.config 文件之中。当程序启动并开始运行时,会率先读取上一次写入的数值。

本文将以模拟量对应通道的设置值以及最大值为例,来实现对应内容的读取与写入操作。

1. 双击打开App.config文件

在<appSettings> 与</appSettings >之间添加需要读写的参数值(注意:需要以键值对的方式,如下文所示)。

<?xml version=1.0 encoding=utf-8?>

<configuration>

<configSections>

<!– For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID= –>

<section name=entityFramework type=System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5ce089 requirePermission=false />

</configSections>

<startup>

<supportedRuntime version=v4.0 sku=.NETFramework,Version=v4.7.2 />

</startup>

<entityFramework>

<providers>

<provider invariantName=System.Data.SqlClient type=System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer />

<provider invariantName=System.Data.SQLite.EF6 type=System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6 />

</providers>

</entityFramework>

<system.data>

<DbProviderFactories>

<remove invariant=System.Data.SQLite.EF6 />

<add name=SQLite Data Provider (Entity Framework 6) invariant=System.Data.SQLite.EF6 description=.NET Framework Data Provider for SQLite (Entity Framework 6) type=System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6 />

<remove invariant=System.Data.SQLite />

<add name=SQLite Data Provider invariant=System.Data.SQLite description=.NET Framework Data Provider for SQLite type=System.Data.SQLite.SQLiteFactory, System.Data.SQLite />

</DbProviderFactories>

</system.data>

<appSettings>

<add key=channel1 value=500 />

<add key=channel2 value=500 />

<add key=channel3 value=500 />

<add key=channel4 value=500 />

<add key=channel5 value=500 />

<add key=channel6 value=500 />

<add key=channel7 value=500 />

<add key=max1 value=500 />

<add key=max2 value=500 />

<add key=max3 value=500 />

<add key=max4 value=500 />

<add key=max5 value=500 />

</appSettings>

</configuration>

2.添加引用

using Configuration = System.Configuration.Configuration;

3. 声明变量

private Configuration config =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

4. 读取内容

//Read System Settings: textBox20/45/48/51/54/57/60 : Display Value

textBox20.Text = ConfigurationManager.AppSettings[“channel1”] ?? “”;

textBox45.Text = ConfigurationManager.AppSettings[“channel2”] ?? “”;

textBox48.Text = ConfigurationManager.AppSettings[“channel3”] ?? “”;

textBox51.Text = ConfigurationManager.AppSettings[“channel4”] ?? “”;

textBox54.Text = ConfigurationManager.AppSettings[“channel5”] ?? “”;

textBox57.Text = ConfigurationManager.AppSettings[“channel6”] ?? “”;

textBox60.Text = ConfigurationManager.AppSettings[“channel7”] ?? “”;

5. 写入数值

通过文本框的KeyPress事件实现

private void textBox20_KeyPress(object sender, KeyPressEventArgs e)

{

string strkey = “channel1”;

if (!char.IsDigit(e.KeyChar) &&

e.KeyChar != (char)Keys.Back &&

e.KeyChar != (char)Keys.Enter)

{

e.Handled = true; //

}

if (e.KeyChar == (char)Keys.Enter)

{

textBox20.ForeColor = Color.Blue;

config.AppSettings.Settings.Remove(“channel1”);

config.AppSettings.Settings.Add(strkey, textBox20.Text);

config.Save(ConfigurationSaveMode.Modified);

ConfigurationManager.RefreshSection(“appSettings”);

}

}

6. 程序运行时,在项目文件夹—bin文件夹,会自动生成以“项目文件名.exe.config”文件,双击可以看到运行结果。

C 对App.config文件读取及写入方法

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/188887.html

(0)
上一篇 2025-09-25 09:26
下一篇 2025-09-25 09:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信