0

各位下午好——

我正在读/写一个由第三方创建和管理的外部文件,该第三方使用 .INI 结构化文件作为其脚本语言。我有一个运行良好的包装器,但是,部分名称是静态的,末尾有一个唯一编号([GENERAL-1]),因此您不止一次执行相同的任务。我正在使用带有 VS2008 的 VB.NET。

我下面的代码可以成功地从硬编码的部分读取密钥,但我希望密钥是通用的。

INI

test.ini
[GENERAL-1]
SUPPRESSTASKERRORS=Y
TASKERRORSENDJOB=Y

代码:

Declare Function GetPrivateProfileString Lib "kernel32.dll" Alias    
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As  
String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As   
Long, ByVal lpFileName As String) As Long

Declare Function WriteProfileString Lib "kernel32.dll" Alias 
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As   
String, ByVal lpString As String, ByVal lpFileName As String) As Long



' Read INI file
    Dim uname As String ' receives the value read from the INI file
    Dim slength As Long ' receives length of the returned string
    Dim OriginalMJB As String = "c:\test\test.ini"
    uname = Space(1024)

slength = GetPrivateProfileString("General-1", "SUPPRESSTASKERRORS", "anonymous", 
uname, 1024, OriginalMJB)

请注意上面的 General-1,如果我将值硬编码为 -1,我可以毫无问题地读取输入的 .ini 文件。关于如何获取和使用连字符左侧的值有什么想法吗?

任何帮助表示赞赏!

- 乔治

4

1 回答 1

1

这是一种方法。从这里您应该能够使 SectionNo 等于您想要的特定部分。

Dim section As String = "General"
Dim SectionNo as String = "-"
Dim Number as Integer = 1
SectionNo += Number.ToString
slength = GetPrivateProfileString(section + SectionNo, "SUPPRESSTASKERRORS", "anonymous", uname, 1024, OriginalMJB)

这里有几个选项

    Dim SectionName As String = "General-1"
    Dim SectionCategorie As String = ""
    Dim Section As String = ""

    'Using Split - It returns an array so you can load the results into an array   
    'or just call it and load the specific index each time.
    SectionCategorie = Split(SectionName, "-")(0)
    Section = Split(SectionName, "-")(1)

    'Using Substring

    SectionCategorie = SectionName.Substring(0, SectionName.IndexOf("-"))
    Section = SectionName.Substring(SectionName.IndexOf("-") + 1)
于 2013-05-29T06:31:20.580 回答