133 lines
4.7 KiB
C#
133 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Windows.Forms;
|
|
using System.IO;
|
|
using System.Runtime.InteropServices;
|
|
|
|
namespace LSFileClient
|
|
{
|
|
public static class IniUtil
|
|
{
|
|
/// <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);
|
|
|
|
/// <summary>
|
|
/// <para>说明:通过Key获取Value值</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-07-22</para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <returns>值</returns>
|
|
public static string ReadFileConfig(string key)
|
|
{
|
|
string value = string.Empty;
|
|
try
|
|
{
|
|
//判读INI文件是否存在
|
|
string filePath = Application.StartupPath + "/MenuConfig.ini";
|
|
if (File.Exists(filePath))
|
|
{
|
|
StringBuilder temp = new StringBuilder(1024);
|
|
string fileName = Path.GetFileNameWithoutExtension(filePath);
|
|
GetPrivateProfileString(fileName, key, "", temp, 1024, filePath);
|
|
|
|
return temp + "";
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
return value;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:通过Key获取Value值</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-07-22</para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <returns>值</returns>
|
|
public static string Read(string iniPath, string key)
|
|
{
|
|
string value = string.Empty;
|
|
try
|
|
{
|
|
//判读INI文件是否存在
|
|
string filePath = iniPath;
|
|
if (File.Exists(filePath))
|
|
{
|
|
StringBuilder temp = new StringBuilder(1024);
|
|
string fileName = Path.GetFileNameWithoutExtension(filePath);
|
|
GetPrivateProfileString(fileName, key, "", temp, 1024, filePath);
|
|
|
|
return temp + "";
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
}
|
|
return value;
|
|
}
|
|
/// <summary>
|
|
/// <para>说明:写入Ini文件(以键值对的形式存放)</para>
|
|
/// <para>创建人:龚宇超</para>
|
|
/// <para>创建日期:2017-07-22</para>
|
|
/// <para>修改人:</para>
|
|
/// <para>修改日期:</para>
|
|
/// <para>修改备注:</para>
|
|
/// <para>版本:1.0</para>
|
|
/// </summary>
|
|
/// <param name="key">键</param>
|
|
/// <param name="value">值</param>
|
|
/// <returns></returns>
|
|
public static void WriteFileConfig(string key, string value)
|
|
{
|
|
try
|
|
{
|
|
//根据INI文件名设置要写入INI文件的节点名称
|
|
//此处的节点名称完全可以根据实际需要进行配置
|
|
string filePath = Application.StartupPath + "/MenuConfig.ini";
|
|
if (!File.Exists(filePath))
|
|
{
|
|
File.Create(filePath);
|
|
}
|
|
string fileName = Path.GetFileNameWithoutExtension(filePath);
|
|
WritePrivateProfileString(fileName, key, value, filePath);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|