简体   繁体   English

带变量段名的读/写INI

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

Good afternoon folks - 大家下午好-

I'm working on reading/writing an external file that is created and managed by a 3rd party that uses .INI structured files as its scripting language. 我正在读取/写入由使用.INI结构化文件作为其脚本语言的第3方创建和管理的外部文件。 I've got a wrapper working pretty well however, the section names are static with a unique number at the end ([GENERAL-1]) so that you have have the same task more than once. 我的包装程序工作得很好,但是节的名称是静态的,末尾有一个唯一的数字([GENERAL-1]),这样您就可以多次执行相同的任务。 I am using VB.NET w/ VS2008. 我正在使用带有VS2008的VB.NET。

My code below can successfully read a key from a section that is hardcoded but I'd like the key to be generic. 我下面的代码可以从硬编码的部分成功读取密钥,但是我希望该密钥具有通用性。

INI 尼尼

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

Code: 码:

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)

Notice the General-1 above, if I have the value hardcoded as -1 I can read the input .ini file without a problem. 请注意上面的General-1,如果我将值硬编码为-1,则可以毫无问题地读取输入的.ini文件。 Any thoughts on how I can get and use the value left of the hyphen? 关于如何获取和使用连字符左值的任何想法?

Any help is appreciated! 任何帮助表示赞赏!

--George - 乔治

Here's one way. 这是一种方法。 From here you should be able to make SectionNo equal the specific section you want. 从这里开始,您应该可以使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)

Here's a couple of options 这有几个选择

    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