在 VBA 中读写 .ini 文件的推荐方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/660138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:41:01  来源:igfitidea点击:

Recommended way to read and write .ini files in VBA

vbams-accessaccess-vbaini

提问by Curtis Inderwiesche

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

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

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

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

...并解析数据。相反,我试图查看已有哪些工具可用。

I know in C# you can do...

我知道在 C# 中你可以做...

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

Is there an equivalent for VBA?

VBA 有等价物吗?

I am attempting this in MS Access 2003 VBA.

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

回答by Birger

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.

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

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

回答by Jon B

It's not pleasant, but you can use the Windows API. Here's a link, and another from MS.

这并不令人愉快,但您可以使用 Windows API。这是一个链接另一个来自 MS

回答by Fionnuala

FileSystemObject objectwith a [TextStream](http://msdn.microsoft.com/en-us/library/314cz14s(VS.85).aspx)is the usually recommended method for reading and writing textfiles in VBA.

带有 [TextStream]( http://msdn.microsoft.com/en-us/library/314cz14s(VS.85).aspx) 的FileSystemObject 对象是在 VBA 中读取和写入文本文件的通常推荐方法。

回答by Mike Woodhouse

Karl Peterson's kpinihas just about everything you're likely to need: API declarations, a Cinifile class, stuff like that. I'd start with that and morph it to taste, which shouldn't take long.

Karl Peterson 的 kpini拥有您可能需要的一切:API 声明、Cinifile 类,诸如此类。我会从那个开始,然后根据口味改变它,这应该不会花很长时间。