繁体   English   中英

带变量段名的读/写INI

[英]Reading/Writing INI w/ variable Section Name

大家下午好-

我正在读取/写入由使用.INI结构化文件作为其脚本语言的第3方创建和管理的外部文件。 我的包装程序工作得很好,但是节的名称是静态的,末尾有一个唯一的数字([GENERAL-1]),这样您就可以多次执行相同的任务。 我正在使用带有VS2008的VB.NET。

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

尼尼

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文件。 关于如何获取和使用连字符左值的任何想法?

任何帮助表示赞赏!

- 乔治

这是一种方法。 从这里开始,您应该可以使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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM