搜档网
当前位置:搜档网 › C#读取ini配置文件

C#读取ini配置文件

Private Declare Function GetPrivateProfileStringByKeyName& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName$, ByVal lpszKey$, ByVal lpszDefault$, ByVal lpszReturnBuffer$, ByVal cchReturnBuffer&, ByVal lpszFile$)
'得到键值
Public Function GetIniValue(Section$, Key$) As String

GetIniValue = String$(128, 0)

GetPrivateProfileStringByKeyName Section, Key, "", GetIniValue, 127, IniFileName
GetIniValue = Replace(GetIniValue, Chr , "")

End Function

'自定义读取INI函數
Public Function ReadIni(ByVal section As String, ByVal key As String) As String
Dim X As Integer
Dim Buff As New VB6.FixedLengthString(128)
Dim i As Short
X = GetPrivateProfileString(section, key, "", Buff.Value, 128, InsPath & "Setup.ini")
i = InStr(Buff.Value, Chr )
ReadIni = Trim(VB.Left(Buff.Value, i - 1))
End Function

using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;

namespace WindowsApplication6
{
///


/// iniClass 的摘要说明。
///

// TODO: 在此处添加构造函数逻辑
public class INIClass
{
public string inipath;
[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);
///
/// 构造方法
///

/// 文件路径
public INIClass(string INIPath)
{
inipath = INIPath;
}
///
/// 写入INI文件
///

/// 项目名称(如 [TypeName] )
///
///
public void IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.inipath);
}
///
/// 读出INI文件
///

/// 项目名称(如 [TypeName] )
///
public string IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(500);
int i = GetPrivateProfileString(Section,Key,"",temp,500,this.inipath);
return temp.ToString();
}
///
/// 验证文件是否存在

///


/// 布尔值
public bool ExistINIFile()
{
return File.Exists(inipath);
}
}

//
}


本文来自CSDN博客,转载请标明出处:https://www.sodocs.net/doc/762988152.html,/wlwqw/archive/2009/01/23/3851464.aspx

相关主题