阅读.ini文件vb.net?

时间:2015-07-14 09:16:05

标签: .net vb.net winapi ini

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

我的代码来读取.ini文件

$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

我的代码加载代码

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

我的.ini文件

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

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

任何人都可以帮我了解如何读取ini文件以加载信息

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

抱歉我的英文不好

3 个答案:

答案 0 :(得分:5)

没有必要滚动你自己的方法来做这件事。使用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

如果使用此方法,请注意一些背景阅读:Could there be encoding-related problems when storing unicode strings in ini files?

答案 1 :(得分:1)

那些建议的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:&#34;您的解决方案似乎不代表代理对。&#34;
Mario Z:&#34;我不知道为什么你认为图书馆不支持Unicode,它确实非常简单......只需使用适当的编码。&#34;

所以在这里,简而言之,.NET的字符串表示Chars数组,Char表示16位字符。当面对包含32位字符的字符串时,它将使用所谓的&#34;代理对&#34; (代表一个的两个字符)。现在,当处理这种字符串时,例如可能发生的问题是,如果我们在该字符串的中间剪切它,那么我们可以创建该字符串的无效子字符串&#34;代理对&#34;。另一个可能发生的问题是使用字符串索引器而不考虑代理对&#34;将包含该字符串中的两个索引字符。

然而,对于MadMilkman.Ini来说,情况并非如此,该库仅使用一组特定字符直接操作,而字符串的其余部分保持原样(字符串是具有完整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")

以下是&#34; textWithSurrogatePairs&#34;的内容。变量:

enter image description here

以下是生成的输出&#34;示例file.ini&#34;文件:

enter image description here

此处还有阅读测试样本:

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

以下是&#34; readValue&#34;变量:

enter image description here

简而言之,.NET框架本身处理代理对,我们唯一需要注意的是使用适当的编码(如上所示)。
不幸的是,这是IInspectable未能实现的,我没有向他正确解释。

答案 2 :(得分:0)

示例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

湾实例化类:

Dim objIniFile As New clsIni(path_of_your_file)

℃。获取设置:

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