引自:http://blog.csdn.net/yizia/archive/2008/06/24/2580744.aspx
在vb.net2005+winxp中,我使用GetPrivateProfileString读取一个ini文件,如果文件路径中含有中文,就会遇到一个奇怪的问题:第一次读取正常,第二次读取就取不到值。代码如下:
- PrivateDeclareFunctionGetPrivateProfileStringLib"kernel32"Alias"GetPrivateProfileStringA"(ByVallpApplicationNameAsString,ByVallpKeyNameAsString,ByVallpDefaultAsString,ByVallpReturnedStringAsString,ByValnSizeAsInteger,ByVallpFileNameAsString)AsInteger
-
- Privatem_filenameAsString="c:/椅子/slcuserinfo.ini"
-
- PublicFunctionGetiniValue(ByVallpKeyNameAsString,ByValstrNameAsString)AsString
-
- DimstrTmpAsString
-
-
-
-
-
- strTmp=NewString("",100)
-
-
-
- CallGetPrivateProfileString(lpKeyName,"",m_filename)
-
-
-
- GetiniValue=Left$(strTmp,InStr(strTmp,vbNullChar)-1)
-
- EndFunction
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,fileTmp)
Call GetPrivateProfileString(lpKeyName,m_filename)
GetiniValue = Left$(strTmp,vbNullChar) - 1)
End Function
反复试验过以后,发现取消注释掉的代码就可以正常运行。百思不得其解,看api的声明没有问题啊,为什么lpFileName会出现异常呢?
不管怎样,这个问题耽误了我2个小时,先贴上故障和解决办法,以后再研究