繁体   English   中英

阅读.ini文件vb.net?

[英]Read .ini file vb.net?

我有一个具有读取.ini文件功能的项目。 我无法显示我想要的.ini文件的内容。

我的代码读取.ini文件

Public Function GetSettingItem(ByVal File As String, ByVal Identifier As String) As String
    Dim S As New IO.StreamReader(File) : Dim Result As String = ""
    Do While (S.Peek <> -1)
        Dim Line As String = S.ReadLine
        If Line.ToLower.StartsWith(Identifier.ToLower & "=") Then
            Result = Line.Substring(Identifier.Length + 2)
        End If
    Loop
    Return Result
End Function

我的代码加载代码

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Label1.Text = GetSettingItem("D:\WorldInfo.ini", "Count")
    Label2.Text = GetSettingItem("D:\WorldInfo.ini", "Count")
 End Sub

我的.ini文件

[B_Empty_IndexList]
Count=2
00_Th=0
01_Th=1
[B_Use_IndexList]
Count=0
00_Th=-1
01_Th=-1

在我的项目中,我想将信息计数从[B_Empty_IndexList]加载到Label1,并将计数从[B_Use_IndexList]加载到Label2 ..但是当我使用代码Label1和Label2时,只是从[B_Use_IndexList]加载计数

有人帮我如何读取ini文件以加载信息

Label1 -> Load Information Count from [B_Empty_IndexList]
Label2 -> Load Information Count from [B_Use_IndecList]

对不起,我的英语不好

滚动自己的方法来执行此操作没有意义。 使用Windows API方法GetPrivateProfileString可以做到这一点:

Imports System.Runtime.InteropServices
Imports System.Text

<DllImport("kernel32")>
Private Shared Function GetPrivateProfileString(ByVal section As String, ByVal key As String, ByVal def As String, ByVal retVal As StringBuilder, ByVal size As Integer, ByVal filePath As String) As Integer
End Function

Public Function GetIniValue(section As String, key As String, filename As String, Optional defaultValue As String = "") As String
    Dim sb As New StringBuilder(500)
    If GetPrivateProfileString(section, key, defaultValue, sb, sb.Capacity, filename) > 0 Then
        Return sb.ToString
    Else
        Return defaultValue
    End If
End Function

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Debug.WriteLine(GetIniValue("B_Empty_IndexList", "Count", My.Application.Info.DirectoryPath & "\WorldInfo.ini"))
    Debug.WriteLine(GetIniValue("B_Use_IndexList", "Count", My.Application.Info.DirectoryPath & "\WorldInfo.ini"))
End Sub

如果使用这种方法,请注意一些背景知识: 在ini文件中存储unicode字符串时,是否存在与编码相关的问题?

那些建议的Windows API已经被弃用了很长时间了,它们确实很糟糕,您会在每个GetPrivateProfile调用中看到正在读取并重新解析INI内容的情况...

或者,尝试使用我的MadMilkman.Ini库,如下所示:

Imports MadMilkman.Ini

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim ini As New IniFile()
    ini.Load("D:\WorldInfo.ini")

    Label1.Text = ini.Sections("B_Empty_IndexList").Keys("Count").Value
    Label2.Text = ini.Sections("B_Use_IndecList").Keys("Count").Value
End Sub

您可以从此处下载该库: https : //github.com/MarioZ/MadMilkman.Ini

编辑
在下面的评论中,我被要求详细说明我与IInspectable进行的讨论。
IInspectable:“您的解决方案似乎无法解决代理对。”
Mario Z:“我不知道为什么您会认为该库不支持Unicode,它确实如此,而且非常简单...仅使用适当的编码即可。”

简而言之,.NET的String表示Chars数组,而Char表示16位字符。 当面对包含32位字符的字符串时,它将使用所谓的“代理对”(两个字符代表一个)。 现在,当使用这种字符串时,可能发生的问题是,例如,如果在“代理对”的中间进行剪切,则可以创建该字符串的无效子字符串。 另外一个可能发生的问题是使用字符串索引器时,没有考虑到“代理对”将由该字符串中的两个索引字符组成。

但是,对于MadMilkman.Ini而言并非如此,该库仅直接使用一组特定的字符进行操作,而其余字符串则保持原样(string是具有完整Unicode支持的自洽类型)。 定位和操纵的字符是[,],=等。

例如,这是一个写作测试样本:

Dim textWithSurrogatePairs =
    "sample content " + Char.ConvertFromUtf32(Int32.Parse("22222", NumberStyles.HexNumber))

Dim ini = New IniFile(
    New IniOptions() With {.Encoding = Encoding.Unicode})

ini.Sections.Add(
    New IniSection(ini, "sample section",
        New IniKey(ini, "sample key", textWithSurrogatePairs)))

ini.Save("sample file.ini")

以下是“ textWithSurrogatePairs”变量的内容:

在此处输入图片说明

以下是生成的输出“ sample file.ini”文件:

在此处输入图片说明

另外这里是阅读测试样本:

Dim ini = New IniFile(
    New IniOptions() With {.Encoding = Encoding.Unicode})

ini.Load("sample file.ini")

Dim readValue = ini.Sections("sample section").Keys("sample key").Value

以下是“ readValue”变量:

在此处输入图片说明

简而言之,.NET框架本身可以处理代理对,我们唯一需要了解的就是使用适当的编码(如上所示)。
不幸的是,这是IInspectable无法实现的,并且我无法正确地向他解释。

示例ini文件:

[MAIN]
Setting_1=something

一种。 创建一个类clsINI

Public Class clsIni
' API functions
Private Declare Ansi Function GetPrivateProfileString _
  Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _
  (ByVal lpApplicationName As String, _
  ByVal lpKeyName As String, ByVal lpDefault As String, _
  ByVal lpReturnedString As System.Text.StringBuilder, _
  ByVal nSize As Integer, ByVal lpFileName As String) _
  As Integer
Private Declare Ansi Function WritePrivateProfileString _
  Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
  (ByVal lpApplicationName As String, _
  ByVal lpKeyName As String, ByVal lpString As String, _
  ByVal lpFileName As String) As Integer
Private Declare Ansi Function GetPrivateProfileInt _
  Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _
  (ByVal lpApplicationName As String, _
  ByVal lpKeyName As String, ByVal nDefault As Integer, _
  ByVal lpFileName As String) As Integer
Private Declare Ansi Function FlushPrivateProfileString _
  Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _
  (ByVal lpApplicationName As Integer, _
  ByVal lpKeyName As Integer, ByVal lpString As Integer, _
  ByVal lpFileName As String) As Integer
Dim strFilename As String

' Constructor, accepting a filename
Public Sub New(ByVal Filename As String)
    strFilename = Filename
End Sub

' Read-only filename property
ReadOnly Property FileName() As String
    Get
        Return strFilename
    End Get
End Property

Public Function GetString(ByVal Section As String, _
  ByVal Key As String, ByVal [Default] As String) As String
    ' Returns a string from your INI file
    Dim intCharCount As Integer
    Dim objResult As New System.Text.StringBuilder(256)
    intCharCount = GetPrivateProfileString(Section, Key, [Default], objResult, objResult.Capacity, strFilename)
    If intCharCount > 0 Then
        GetString = Left(objResult.ToString, intCharCount)
    Else
        GetString = ""
    End If

End Function

Public Function GetInteger(ByVal Section As String, _
  ByVal Key As String, ByVal [Default] As Integer) As Integer
    ' Returns an integer from your INI file
    Return GetPrivateProfileInt(Section, Key, _
       [Default], strFilename)
End Function

Public Function GetBoolean(ByVal Section As String, _
  ByVal Key As String, ByVal [Default] As Boolean) As Boolean
    ' Returns a boolean from your INI file
    Return (GetPrivateProfileInt(Section, Key, _
       CInt([Default]), strFilename) = 1)
End Function

Public Sub WriteString(ByVal Section As String, _
  ByVal Key As String, ByVal Value As String)
    ' Writes a string to your INI file
    WritePrivateProfileString(Section, Key, Value, strFilename)
    Flush()
End Sub

Public Sub WriteInteger(ByVal Section As String, _
  ByVal Key As String, ByVal Value As Integer)
    ' Writes an integer to your INI file
    WriteString(Section, Key, CStr(Value))
    Flush()
End Sub

Public Sub WriteBoolean(ByVal Section As String, _
  ByVal Key As String, ByVal Value As Boolean)
    ' Writes a boolean to your INI file
    WriteString(Section, Key, CStr(CInt(Value)))
    Flush()
End Sub

Private Sub Flush()
    ' Stores all the cached changes to your INI file
    FlushPrivateProfileString(0, 0, 0, strFilename)
End Sub
End Class

b。 实例化该类:

Dim objIniFile As New clsIni(path_of_your_file)

C。 获取设置:

Dim x As String
x = objIniFile.GetString("MAIN", "Setting_1", "")

d。 更新/创建设置:

objIniFile.WriteString("MAIN", "Setting_1", "new_setting")

编辑:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim objIniFile As New clsIni("D:\WorldInfo.ini")
    Label1.Text = objIniFile.GetString("B_Empty_IndexList", "Count", "")
    Label2.Text = objIniFile.GetString("B_Use_IndexList", "Count", "")
End Sub

暂无
暂无

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

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