|
- using System;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Text;
- namespace IniFile
- {
- /// <summary>
- /// ini文件读与写
- /// </summary>
- public class ClsIniFile
- {
- //文件INI名称
- public string Path;
- ////声明读写INI文件的API函数
- [DllImport("kernel32")]
- private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
- [DllImport("kernel32")]
- private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
- //类的构造函数,传递INI文件名
- public ClsIniFile(string inipath){ Path = inipath; }
- /// <summary>
- /// 写入INI文件
- /// </summary>
- /// <param name="Section">配置节</param>
- /// <param name="Key">键名</param>
- /// <param name="Value">键值</param>
- public void IniWriteValue(string Section, string Key, string Value)
- {
- WritePrivateProfileString(Section, Key, Value, this.Path);
- }
- /// <summary>
- /// 读取制定INI文件键值
- /// </summary>
- /// <param name="Section">配置节</param>
- /// <param name="Key">键名</param>
- /// <returns></returns>
- public string IniReadValue(string Section, string Key)
- {
- StringBuilder temp = new StringBuilder(255);
- int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.Path);
- return temp.ToString();
- }
- }
- }
复制代码 INI文件简单写于读类- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using IniFile;//ini简单类
- namespace Winform_读写INI文件
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void but_Click(object sender, EventArgs e)
- {
- string struser = this.user.Text.Trim(), strpwd = this.pwd.Text.Trim();
- #region 写入INI
- string sPath = Application.StartupPath + "\\FondoServer.ini";//写入到软件当前目录
- ClsIniFile cl = new ClsIniFile(sPath);
- cl.IniWriteValue("FondoServer", "user", struser);
- cl.IniWriteValue("FondoServer", "pwd", strpwd);
- /*多个如下添加就是咯*/
- // cl.IniWriteValue("FondoServer", "user", struser);
- // cl.IniWriteValue("FondoServer", "pwd", strpwd);
- MessageBox.Show("INI文件写入成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- #endregion
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- ReadDataini();
-
- }
- private void ReadDataini()
- {
- string sPath = Application.StartupPath + "\\FondoServer.ini";//读取软件当前目录
- ClsIniFile ini = new ClsIniFile(sPath);
- this.user.Text = ini.IniReadValue("FondoServer", "user");
- this.pwd.Text = ini.IniReadValue("FondoServer", "pwd");
- /*多个如下添加就是咯*/
- //this.user.Text = ini.IniReadValue("FondoServer", "user");
- //this.pwd.Text = ini.IniReadValue("FondoServer", "pwd");
-
- }
- private void linkLabel1_Click(object sender, EventArgs e)
- {
-
- System.Diagnostics.Process.Start("http://www.sufeinet.com");
- }
-
- }
- }
复制代码
效果图
源码
|
本帖被以下淘专辑推荐:
- · 源码下载|主题: 25, 订阅: 4
- · 文件操作|主题: 14, 订阅: 0
|