阅读.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
如果使用这种方法,请注意一些背景知识: My code to load code my .ini files in my project i wanna load information Count from [B_Empty_IndexList] to Label1 and Count from [B_Use_IndexList] to Label2 .. but when i use my code Label1 and Label2 just load Count from [B_Use_IndexList] Anyone help me how to read ini file to load information Sorry for my bad english There is no point rolling your own method to do this. Use the Windows API method GetPrivateProfileString to do it: Note some background reading if using this approach: Could there be encoding-related problems when storing unicode strings in ini files? 这篇关于阅读.ini文件vb.net?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!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]
Count=2
00_Th=0
01_Th=1
[B_Use_IndexList]
Count=0
00_Th=-1
01_Th=-1
Label1 -> Load Information Count from [B_Empty_IndexList]
Label2 -> Load Information Count from [B_Use_IndecList]
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