当程序员们在使用VB.NET编程语言进行程序开发的时候,会逐渐的发现,这款语言给他们带来了不一样的编程体验,极大的提高了开发效率。首先就让我们从VB.NET访问INI文件的实现方法中来对此做一个解读。
在VB.NET2005+winxp中,我使用GetPrivateProfileString实现VB.NET访问INI文件这一功能,如果文件路径中含有中文,就会遇到一个奇怪的问题:***次读取正常,第二次读取就取不到值
VB.NET访问INI文件代码如下:
- 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 Integer, ByVal lpFileName As String) As Integer- Private m_filename As String =
"c:\椅子\slcuserinfo.INI"- Public Function GetINIValue(ByVal lpKeyName
As String, ByVal strName As String) As String- Dim strTmp As String
- 'Dim fileTmp As String
- 'fileTmp = m_filename
- strTmp = New String(" ", 100)
- 'Call GetPrivateProfileString(lpKeyName,
strName, "", strTmp, Len(strTmp), fileTmp)- Call GetPrivateProfileString(lpKeyName,
strName, "", strTmp, Len(strTmp), m_filename)- GetINIValue = Left$(strTmp, InStr(strTmp,
vbNullChar) - 1)- End Function
- 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 Integer, ByVal lpFileName As String) As Integer- Private m_filename As String = "c:\椅子\
slcuserinfo.INI"- Public Function GetINIValue(ByVal lpKeyName
As String, ByVal strName As String) As String- Dim strTmp As String
- 'Dim fileTmp As String
- 'fileTmp = m_filename
- strTmp = New String(" ", 100)
- 'Call GetPrivateProfileString(lpKeyName,
strName, "", strTmp, Len(strTmp), fileTmp)- Call GetPrivateProfileString(lpKeyName,
strName, "", strTmp, Len(strTmp), m_filename)- GetINIValue = Left$(strTmp, InStr(strTmp,
vbNullChar) - 1)- End Function
反复试验过以后,发现取消注释掉的代码就可以正常运行。这就是我们为大家介绍的VB.NET访问INI文件具体实现方法。
【编辑推荐】