可以采用此方法,请参考用一下。希望对大家有用,可以读中文、日文、英文路径
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
本文介绍了一种在不同路径下(包括中文路径)读取和写入INI文件的方法,并提供了具体的VB.NET代码实现,包括获取上一次操作的账号信息及保存默认值的功能。
2333

被折叠的 条评论
为什么被折叠?



