VB.net GetPrivateProfileString WritePrivateProfileString 读写INI

来源:互联网 发布:json解析天气预报 编辑:程序博客网 时间:2024/05/17 12:52

 

可以采用此方法,请参考用一下。希望对大家有用,可以读中文、日文、英文路径


 Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Int32

Private Function GetPreviousInfo(ByRef sPreActNumber As String, ByRef sPreUserName As String) As Boolean
        Try

            Dim strPath As String
            strPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).ToString
            sPreActNumber = Space(255)
            sPreUserName = Space(255)
            GetPrivateProfileString("SMS", "ActNumber", "", sPreActNumber, 128, strPath & iniFileName)
            GetPrivateProfileString("SMS", "UserName", "", sPreUserName, 128, strPath & iniFileName)


            GetPreviousInfo = True

        Catch ex As Exception
            MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
            GetPreviousInfo = False

        End Try

    End Function

Private Function SaveDefaultValue() As Boolean

        Try
            Dim strPath As String
            strPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments).ToString
            Dim sPreActNumber As String
            Dim sPreUserName As String

            sPreActNumber = Mid(Me.Cmb_AccountList.Text, 1, InStr(Me.Cmb_AccountList.Text, "|") - 2)
            sPreUserName = Me.Txt_UserName.Text


          
            Dim fs As System.IO.FileStream


            If System.IO.File.Exists(strPath & iniFileName) Then
                System.IO.File.Delete(strPath & iniFileName)
            End If
            fs = System.IO.File.Create(strPath & iniFileName)
            fs.Close()

            WritePrivateProfileString("SMS", "ActNumber", sPreActNumber, strPath & iniFileName)
            WritePrivateProfileString("SMS", "UserName", sPreUserName, strPath & iniFileName)


            SaveDefaultValue = True

        Catch ex As Exception
            MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
            SaveDefaultValue = False

        End Try
    End Function

原创粉丝点击