首页 > Windows开发 > 详细

C# 读写ini文件

时间:2015-11-19 10:56:37      阅读:415      评论:0      收藏:0      [点我收藏+]

1.添加引用

using System.IO;
using System.Runtime.InteropServices;

2.声明API函数

1     #region  API函数声明
2     [DllImport("kernel32")]
3     private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);
4     [DllImport("kernel32")]
5     private static extern long GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filepath);
6     #endregion

3.文件读取的方法

1     private string ContentValue(string Section, string key, string strFilePath)
2     {
3         StringBuilder temp = new StringBuilder(1024);
4         GetPrivateProfileString(Section, key, "", temp, 1024, strFilePath);
5         return temp.ToString();
6     }

4.读取文件实例

1         private void button1_Click(object sender, EventArgs e)
2         {
3             string path = Application.StartupPath + @"\配置文件.ini";
4             textBox1.Text = ContentValue("Server", "path", path);
5             textBox2.Text = ContentValue("Server", "cmd", path);
6             textBox3.Text = ContentValue("Client", "path", path);
7             textBox4.Text = ContentValue("Client", "cmd", path);
8         }

效果图:

技术分享

C# 读写ini文件

原文:http://www.cnblogs.com/imstrive/p/4976687.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!