推荐的读写.ini文件的方法 [英] Recommended way to read and write .ini files

查看:140
本文介绍了推荐的读写.ini文件的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VBA中是否有任何方法可以读取和写入INI文件?我知道我可以使用;

Are any methods available in VBA to read and write INI files? I know I could use;

Open "C:\test.ini" For Input As #1

...然后解析数据.相反,我试图查看哪些工具已经可用.

...and parse the data. Instead I am trying to see what tools are already available.

我知道在C#中您可以做到...

I know in C# you can do...

 using INI;
 INIFile ini = new INIFile("C:\test.ini");

VBA是否等效?

我正在MS Access 2003 VBA中尝试此操作.

I am attempting this in MS Access 2003 VBA.

推荐答案

以下是我们使用的一些代码段,它可以帮助您理解.这些例程使用API​​调用.包含两个函数,用于将字符串设置读/写到ini文件中的特定部分.

Here are some code snippets that we use, it should help you to get the idea. These routines use the API calls. Two functions are included to read / write a string setting to a specific section in the ini file.

Private 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
Private 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 IniFileName() As String
  IniFileName = "c:\[yourpath here]\settings.ini"
End Function


Private Function ReadIniFileString(ByVal Sect As String, ByVal Keyname As String) As String
Dim Worked As Long
Dim RetStr As String * 128
Dim StrSize As Long

  iNoOfCharInIni = 0
  sIniString = ""
  If Sect = "" Or Keyname = "" Then
    MsgBox "Section Or Key To Read Not Specified !!!", vbExclamation, "INI"
  Else
    sProfileString = ""
    RetStr = Space(128)
    StrSize = Len(RetStr)
    Worked = GetPrivateProfileString(Sect, Keyname, "", RetStr, StrSize, IniFileName)
    If Worked Then
      iNoOfCharInIni = Worked
      sIniString = Left$(RetStr, Worked)
    End If
  End If
  ReadIniFileString = sIniString
End Function

Private Function WriteIniFileString(ByVal Sect As String, ByVal Keyname As String, ByVal Wstr As String) As String
Dim Worked As Long

  iNoOfCharInIni = 0
  sIniString = ""
  If Sect = "" Or Keyname = "" Then
    MsgBox "Section Or Key To Write Not Specified !!!", vbExclamation, "INI"
  Else
    Worked = WritePrivateProfileString(Sect, Keyname, Wstr, IniFileName)
    If Worked Then
      iNoOfCharInIni = Worked
      sIniString = Wstr
    End If
    WriteIniFileString = sIniString
  End If
End Function

这篇关于推荐的读写.ini文件的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆