转自 百度知道
C# 读写 ini配置文件 点此链接
'API 声明
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
'声明结束
Public Function WriteToIni(ByVal FileName As String, ByVal Section As String, ByVal Key As String, ByVal Value As String) As Boolean
Dim buff As String * 128
buff = Value + Chr(0)
WriteToIni = IIf(WritePrivateProfileString(Section, Key, buff, FileName) = 1, True, False)
End Function
'写入(Section不带中括号),Value是写入值(最好先CStr成String),返回“True”写入成功,反之失败
Public Function ReadFromIni(ByVal FileName As String, ByVal Section As String, ByVal Key As String) As String
Dim I As Long
Dim buff As String * 128
If GetPrivateProfileString(Section, Key, "", buff, 128, FileName) = 0 Then ReadFromIni = "": Exit Function
I = InStr(buff, Chr(0))
ReadFromIni = Trim(Left(buff, I - 1))
End Function