我有一个项目具有读取.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
Run Code Online (Sandbox Code Playgroud)
我的代码加载代码
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
Run Code Online (Sandbox Code Playgroud)
我的.ini文件
[B_Empty_IndexList]
Count=2
00_Th=0
01_Th=1
[B_Use_IndexList]
Count=0
00_Th=-1
01_Th=-1
Run Code Online (Sandbox Code Playgroud)
在我的项目中,我想加载信息从[B_Empty_IndexList]到Label1和Count从[B_Use_IndexList]加载到Label2 ..但是当我使用我的代码Label1和Label2时只需从[B_Use_IndexList]加载Count
任何人都帮助我如何读取ini文件来加载信息
Label1 -> Load Information Count from [B_Empty_IndexList]
Label2 -> Load Information Count from [B_Use_IndecList]
Run Code Online (Sandbox Code Playgroud)
对不起,我的英语不好
没有必要滚动你自己的方法来做到这一点.使用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
Run Code Online (Sandbox Code Playgroud)
如果使用这种方法,请注意一些背景读取:在ini文件中存储unicode字符串时是否会出现与编码相关的问题?
| 归档时间: |
|
| 查看次数: |
19532 次 |
| 最近记录: |