C#读写INI文件
INI文件格式
[StartUp]
StartKey=StartValue
[sectionName]
KeyName=KeyValue
StartKey=StartValue
[sectionName]
KeyName=KeyValue
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.Diagnostics;
using Microsoft.Win32;
using System.IO;
using System.Runtime.InteropServices;
namespace UseINI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(GetPrivateProfileString("sectionName", "KeyName"));
}
/// <summary>
/// 软件安装路径,以此exe所在的路径为准。
/// </summary>
string installDirectory = null;
private string InstallDirectory
{
get
{
if (string.IsNullOrEmpty(installDirectory))
{
string sPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
installDirectory = Path.GetDirectoryName(sPath) + @"\";
}
return installDirectory;
}
}
private string startUpIniFileName = null;
public string StartUpIniFileName
{
get
{
if (string.IsNullOrEmpty(startUpIniFileName))
{
startUpIniFileName = InstallDirectory + "StartUp.ini";
}
return startUpIniFileName;
}
}
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
/// <summary>
/// 从Ini文件获取数据
/// </summary>
/// <param name="section">应用程序</param>
/// <param name="key">键的名称</param>
/// <returns>键的值</returns>
private string GetPrivateProfileString(string section, string key)
{
int nCapacity = 255;
StringBuilder temp = new StringBuilder(nCapacity);
int i = GetPrivateProfileString(section, key, "", temp, nCapacity, StartUpIniFileName);
if (i < 0)
return "";
return temp.ToString();
}
/// <summary>
/// 向Ini文件中写入值
/// </summary>
/// <param name="section">应用程序</param>
/// <param name="key">键的名称</param>
/// <param name="value">键的值</param>
/// <returns>执行成功为True,失败为False。</returns>
public long WritePrivateProfileString(string section, string key, string value)
{
if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0)
return 0;
return WritePrivateProfileString(section, key, value, StartUpIniFileName);
}
}
}
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.Diagnostics;
using Microsoft.Win32;
using System.IO;
using System.Runtime.InteropServices;
namespace UseINI
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(GetPrivateProfileString("sectionName", "KeyName"));
}
/// <summary>
/// 软件安装路径,以此exe所在的路径为准。
/// </summary>
string installDirectory = null;
private string InstallDirectory
{
get
{
if (string.IsNullOrEmpty(installDirectory))
{
string sPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
installDirectory = Path.GetDirectoryName(sPath) + @"\";
}
return installDirectory;
}
}
private string startUpIniFileName = null;
public string StartUpIniFileName
{
get
{
if (string.IsNullOrEmpty(startUpIniFileName))
{
startUpIniFileName = InstallDirectory + "StartUp.ini";
}
return startUpIniFileName;
}
}
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
/// <summary>
/// 从Ini文件获取数据
/// </summary>
/// <param name="section">应用程序</param>
/// <param name="key">键的名称</param>
/// <returns>键的值</returns>
private string GetPrivateProfileString(string section, string key)
{
int nCapacity = 255;
StringBuilder temp = new StringBuilder(nCapacity);
int i = GetPrivateProfileString(section, key, "", temp, nCapacity, StartUpIniFileName);
if (i < 0)
return "";
return temp.ToString();
}
/// <summary>
/// 向Ini文件中写入值
/// </summary>
/// <param name="section">应用程序</param>
/// <param name="key">键的名称</param>
/// <param name="value">键的值</param>
/// <returns>执行成功为True,失败为False。</returns>
public long WritePrivateProfileString(string section, string key, string value)
{
if (section.Trim().Length <= 0 || key.Trim().Length <= 0 || value.Trim().Length <= 0)
return 0;
return WritePrivateProfileString(section, key, value, StartUpIniFileName);
}
}
}
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。