VB6分类搜索功能

问题描述:

如果我有一个包含以下信息的文本文件,搜索该版本的最佳方式是什么,例如仅在system1上? (?在VB6的代码,我可以用InStrVB6分类搜索功能

 
[system1] 

version=xx 
date=xx 


[system2] 

version=xx 
date=xx 
+2

请参阅http://*.com/q/165796/11683。 – GSerg 2013-02-25 21:28:20

假设有版本后日期前一个空格(STR将你的文字):

Dim version As String 
version = Mid(str, InStr(str, "version") + 8) 
version = Mid(version, 1, InStr(version, " ")) 

但也有一堆函数来解析.ini文件。

+0

如果我只想要[system2]下的版本,你会发现第一次出现'版本'不会呢? – user1179317 2013-02-26 01:48:36

+0

您可以先找到版本的第二个索引或找到system2。 – 2013-02-26 02:15:29

+0

@HanletEscaño,但它可能不会在第二个'[SystemX]'组中。 – Deanna 2013-02-27 08:53:37

我创建一个文本文件"C:\MYFILE.TXT",这是该文件的内容:

[system1] 

version=aa 
date=bb 


[system2] 

version=yy 
date=zz 

[system3] 

date=4 

[system4] 

[system5] 

正如你所看到的,我做了所有可能的情况,即。有版本信息,没有版本信息系统等系统。然后我写了这个代码:

这里是寻找下一个分类列表中的值的一种方法:

Dim mySystem As String 
Dim myVersion As String 

mySystem = "[system2]" 'Replace this with the system you are looking for 

Dim strTmp As String 
Dim SystemFound As Boolean 
Dim VersionFound As Boolean 

Open "c:\myfilename.txt" For Input As #1 
    Do While Not EOF(1) 
     Line Input #1, strTmp 
     If InStr(UCase(strTmp), UCase(mySystem)) > 0 Then 
      SystemFound = True 
      strTmp = "" 
      Do While Not EOF(1) And InStr(strTmp, "[") = 0 
       Line Input #1, strTmp 
       If InStr(UCase(strTmp), "VERSION") > 0 Then 
        VersionFound = True 
        myVersion = Mid(strTmp, InStr(strTmp, "=") + 1, Len(strTmp)) 

       End If 
      Loop 
      Exit Do 
     End If 
    Loop 
Close #1 

If SystemFound And VersionFound Then 
    MsgBox "The Version of " & mySystem & " is " & myVersion 
ElseIf SystemFound And Not VersionFound Then 
    MsgBox "The system " & mySystem & " has no version definition" 
ElseIf SystemFound = False Then 
    MsgBox "The system " & mySystem & " is not found in file" 
End If 

样本文件看起来就像一个标准的INI文件。您可以使用GetPrivateProfileString()和相关功能来阅读这些内容。您还可以使用GetPrivateProfileSectionNames()GetPrivateProfileSection()来枚举部分和值。

+1

+1 Karl E Peterson的入门课程很好http://vb.mvps.org/samples/kpIni/ – MarkJ 2013-02-26 20:57:05