Read .ini file vb.net?

前端 未结 3 849
青春惊慌失措
青春惊慌失措 2021-01-07 10:00

I have one project with function to read .ini files. I can not display the contents of .ini file that I want to.

My code to read .ini file

Public Fun         


        
3条回答
  •  情话喂你
    2021-01-07 10:40

    There is no point rolling your own method to do this. Use the Windows API method GetPrivateProfileString to do it:

    Imports System.Runtime.InteropServices
    Imports System.Text
    
    
    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
    

    Note some background reading if using this approach: Could there be encoding-related problems when storing unicode strings in ini files?

提交回复
热议问题