WritePrivateProfileString GetPrivateProfileString 读取写 配置文件

 

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

 

 

GetPrivateProfileString   详解

  说明
  为初始化文件中指定的条目取得字串
  返回值
  Long,复制到lpReturnedString缓冲区的字节数量,其中不包括那些NULL中止字符。如lpReturnedString缓冲区不够大,不能容下全部信息,就返回nSize-1(若lpApplicationName或lpKeyName为NULL,则返回nSize-2)
  参数表
  参数 类型及说明
  lpApplicationName String,欲在其中查找条目的小节名称。这个字串不区分大小写。如设为vbNullString,就在lpReturnedString缓冲区内装载这个ini文件所有小节的列表
  lpKeyName String,欲获取的项名或条目名。这个字串不区分大小写。如设为vbNullString,就在lpReturnedString缓冲区内装载指定小节所有项的列表
  lpDefault String,指定的条目没有找到时返回的默认值。可设为空("")
  lpReturnedString String,指定一个字串缓冲区,长度至少为nSize
  nSize Long,指定装载到lpReturnedString缓冲区的最大字符数量
  lpFileName String,初始化文件的名字。如没有指定一个完整路径名,windows就在Windows目录中查找文件
  注解
  如lpKeyName参数为vbNullString,那么lpReturnedString缓冲区会载入指定小节所有设置项的一个列表。每个项都用一个NULL字符分隔,最后一个项用两个NULL字符中止。也请参考GetPrivateProfileInt函数的注解

-------------------------------------------

 

例子

清空配置文件内容

Public Const LogFileName = "\Conn.ini"

 

Open App.Path & LogFileName For Output As #1
Print #1, ""
Close #1

 

 

往配置文件添加内容

Dim txt As String
Dim tmpstr1 As String
txt = "1"
tmpstr1 = "111111111111111" & "##" & "222222"
'' WritePrivateProfileString "配置信息", 0&, 0&, App.Path & LogFileName
WriteInifile "配置信息", tmpstr1, txt, App.Path & LogFileName

 

 

 

获取配置文件中的对应字段的信息

Dim tmpstr1 As String
Dim tmpstr2 As String
Dim tmpstr3 As String
Dim upFlag As Integer
tmpstr3 = frmMain.UserFlex.Cell(aRow, 0).Text

tmpstr1 = tmpUpLoadData.UpStructData(1).XMBH & "##" & tmpstr3


tmpstr2 = ""
tmpstr2 = GetIniStr("配置信息", tmpstr1, App.Path & LogFileName)
If tmpstr2 <> "" Then
upFlag = CInt(tmpstr2)

End If
If upFlag = 1 Then
MsgBox ("该试验已经上传,不能再重新做试验")
Exit Sub
End If

 

 

 

---------

Public Function WriteInifile(ByVal Section As String, ByVal InKey As String, _
ByVal InData As String, ByVal FileName As String) As Boolean
On Error GoTo WriteInifileErr
WriteInifile = True
WritePrivateProfileString Section, InKey, InData, FileName
Exit Function
WriteInifileErr:
Err.Clear
WriteInifile = False
End Function

Public Function GetIniStr(ByVal AppName As String, ByVal InKey As String, ByVal FileName As String) As String
On Error GoTo GetIniStrErr

If Len(Trim(InKey)) = 0 Then
GoTo GetIniStrErr
End If

Dim GetStr As String

GetStr = String(256, 0)
GetPrivateProfileString AppName, InKey, "", GetStr, 256, FileName
GetStr = Replace(GetStr, Chr(0), "")

If Len(GetStr) = 0 Then
GoTo GetIniStrErr
Else
GetIniStr = GetStr
GetStr = ""
End If

Exit Function
GetIniStrErr:
' Err.Clear
GetIniStr = ""
GetStr = ""
End Function

你可能感兴趣的:(WritePrivateProfileString GetPrivateProfileString 读取写 配置文件)