C# 操作 INI 自己工作笔记(对文本框的操作)

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.IO;
using System.Runtime.InteropServices;

namespace GHGD.UI
{
public partial class FrmBackUp : Form
{
public FrmBackUp()
{
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 + "\BackUpIni.ini";//获取INI文件路径
private string strFilePath = Application.StartupPath + "\" + "BackUpIni.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", txt_ServerName.Text.Trim(), strFilePath);
// WritePrivateProfileString(strSec, "Sex", txt_DatabaseName.Text.Trim(), strFilePath);
// WritePrivateProfileString(strSec, "Age", txt_UserName.Text.Trim(), strFilePath);
// WritePrivateProfileString(strSec, "Address", txt_Password.Text.Trim(), strFilePath);
// WritePrivateProfileString(strSec, "Address", txt_BackUpPath.Text.Trim(), strFilePath);
// MessageBox.Show("写入成功");
// }catch(Exception ex){
// MessageBox.Show(ex.Message.ToString());
// }
//}

//读取按钮事件
//private void btnRead_Click(object sender, EventArgs e)
//{
//if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在
//{
// //[BackUpIni]
// strSec = Path.GetFileNameWithoutExtension(strFilePath);
// txt_ServerName.Text = ContentValue(strSec, "ServerName");
// txt_DatabaseName.Text = ContentValue(strSec, "DatabaseName");
// txt_UserName.Text = ContentValue(strSec, "UserName");
// txt_Password.Text = ContentValue(strSec, "Password");
// txt_BackUpPath.Text = ContentValue(strSec, "BackUpPath");
//}
//else {
// MessageBox.Show("INI文件不存在");
//}
//}


/// <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);
int i = GetPrivateProfileString(Section, key, "", temp, 1024, strFilePath);
return temp.ToString();
}

private void FrmBackUp_Load(object sender, EventArgs e)
{
//MessageBox.Show(strFilePath);
if (File.Exists(strFilePath))//读取时先要判读INI文件是否存在
{
//[BackUpIni]
strSec = Path.GetFileNameWithoutExtension(strFilePath);
txt_ServerName.Text = ContentValue(strSec, "ServerName");
txt_DatabaseName.Text = ContentValue(strSec, "DatabaseName");
txt_UserName.Text = ContentValue(strSec, "UserName");
txt_Password.Text = ContentValue(strSec, "Password");
txt_BackUpPath.Text = ContentValue(strSec, "BackUpPath");
}
else
{
//类的构造函数,传递INI文件名
//public IniFiles(string AFileName)
//{
       // 判断文件是否存在
FileInfo fileInfo = new FileInfo(strFilePath);
       if ((!fileInfo.Exists))
       { //|| (FileAttributes.Directory in fileInfo.Attributes))
        //文件不存在,建立文件
System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, System.Text.Encoding.Default);
        try
        {
           sw.Write("#备份数据文档");
           sw.Close();
        }
        catch
        {
           throw (new ApplicationException("Ini文件不存在"));
        }
       }
      //必须是完全路径,不能是相对路径
strFilePath = fileInfo.FullName;
MessageBox.Show("strFilePath: " + strFilePath);

strSec = Path.GetFileNameWithoutExtension(strFilePath);
MessageBox.Show("strSec: " + strSec);
txt_ServerName.Text = ContentValue(strSec, "ServerName");
txt_DatabaseName.Text = ContentValue(strSec, "DatabaseName");
txt_UserName.Text = ContentValue(strSec, "UserName");
txt_Password.Text = ContentValue(strSec, "Password");
txt_BackUpPath.Text = ContentValue(strSec, "BackUpPath");

//}
//MessageBox.Show("INI文件不存在");
}
}

private void btn_BackUp_ok_Click(object sender, EventArgs e)
{
try
{
//根据INI文件名设置要写入INI文件的节点名称
//此处的节点名称完全可以根据实际需要进行配置
WritePrivateProfileString("BackUpIni", "ServerName", txt_ServerName.Text.Trim(), strFilePath);
WritePrivateProfileString("BackUpIni", "DatabaseName", txt_DatabaseName.Text.Trim(), strFilePath);
WritePrivateProfileString("BackUpIni", "UserName", txt_UserName.Text.Trim(), strFilePath);
WritePrivateProfileString("BackUpIni", "Password", txt_Password.Text.Trim(), strFilePath);
WritePrivateProfileString("BackUpIni", "BackUpPath", txt_BackUpPath.Text.Trim(), strFilePath);
MessageBox.Show("写入成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
this.Close();
}

private void btn_BackUp_cancle_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

原文地址:https://www.cnblogs.com/meimao5211/p/3339120.html