winfrom 操作 INI 文件

来源:互联网 发布:环境软件系统集成商 编辑:程序博客网 时间:2024/05/17 10:38
<strong><span style="font-size:18px;">(1)INI文件的名称:FileConfig.ini</span></strong>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:14px;"><strong>(2)实现代码</strong></span></span>
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;using System.Runtime.InteropServices;   //加上两个引用using System.IO;namespace AboutIni {    public partial class INI : Form {        public INI() {            InitializeComponent();        }        #region "声明变量"        /// <summary>        /// 写入INI文件        /// </summary>        /// <param name="section">节点名称[如[TypeName]]</param>        /// <param name="key">键</param>        /// <param name="val">值</param>        /// <param name="filepath">文件路径</param>        /// <returns></returns>        [DllImport("kernel32")]        private static extern long WritePrivateProfileString(string section, string key, string val, string filepath);        /// <summary>        /// 读取INI文件        /// </summary>        /// <param name="section">节点名称</param>        /// <param name="key">键</param>        /// <param name="def">值</param>        /// <param name="retval">stringbulider对象</param>        /// <param name="size">字节大小</param>        /// <param name="filePath">文件路径</param>        /// <returns></returns>        [DllImport("kernel32")]        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retval, int size, string filePath);        private string strFilePath = Application.StartupPath + "\\FileConfig.ini";//获取INI文件路径        private string strSec = "";//INI文件名              #endregion        //写入         private void btnWrite_Click(object sender, EventArgs e) {                          try            {                    //根据INI文件名设置要写入INI文件的节点名称                    //此处的节点名称完全可以根据实际需要进行配置                    strSec = Path.GetFileNameWithoutExtension(strFilePath);                    WritePrivateProfileString(strSec, "Name", txtName.Text.Trim(), strFilePath);                    WritePrivateProfileString(strSec, "Sex", txtSex.Text.Trim(), strFilePath);                    WritePrivateProfileString(strSec, "Age", txtAge.Text.Trim(), strFilePath);                    WritePrivateProfileString(strSec, "Address", txtAddress.Text.Trim(), strFilePath);                    MessageBox.Show("写入成功");                         }catch(Exception ex){                MessageBox.Show(ex.Message.ToString());                return;            }                }        //读取         private void btnRead_Click(object sender, EventArgs e) {             if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在            {                 strSec = Path.GetFileNameWithoutExtension(strFilePath);                 txtName.Text = ContentValue(strSec, "Name");                 txtSex.Text = ContentValue(strSec, "Sex");                 txtAge.Text = ContentValue(strSec, "Age");                 txtAddress.Text = ContentValue(strSec, "Address");             } else {                 MessageBox.Show("INI文件不存在");                 return;             }         }         /// <summary>         /// 自定义读取INI文件中的内容方法         /// </summary>         /// <param name="Section">键</param>         /// <param name="key">值</param>         /// <returns></returns>         private string ContentValue(string Section, string key) {             StringBuilder temp = new StringBuilder(1024);             GetPrivateProfileString(Section, key, "", temp, 1024, strFilePath);             return temp.ToString();         }    }}

(3)图片


1 0