【发布时间】:2018-04-09 21:36:43
【问题描述】:
使用 Visual Basic 从 Unicode INI 文档中读取 INI 值。
我认为这是正确的:
Private Class NativeMethods
' AJT 2014.11.28 Use the unicode function !!!
<DllImport("kernel32", CharSet:=CharSet.Unicode)>
Public Shared Function WritePrivateProfileString(section As String, key As String, val As String, filePath As String) As Boolean
End Function
' AJT 2014.11.28 Use the unicode function !!!
<DllImport("kernel32", CharSet:=CharSet.Unicode)>
Public Shared Function GetPrivateProfileString(section As String, key As String, def As String, retVal As StringBuilder, size As Integer, filePath As String) As Integer
End Function
End Class
但是当我从 INI 文件中读取俄语数据时,我得到了问号。
然后我偶然发现了this,它定义了:
Private Declare Function GetPrivateProfileString _ Lib "kernel32" Alias "GetPrivateProfileStringA" _ (ByVal lpSectionName As String, _ ByVal lpKeyName As Any, _ ByVal lpDefault As String, _ ByVal lpbuffurnedString As String, _ ByVal nBuffSize As Long, _ ByVal lpFileName As String) As Long
我注意到他们使用了GetPrivateProfileStringA,这让我觉得我可能需要使用GetPrivateProfileStringW,但现在我很困惑,因为我正在使用:
<DllImport("kernel32", CharSet:=CharSet.Unicode)>
请帮忙。
【问题讨论】:
-
这是一个严格的遗留 API,保留用于重现 Windows v3 行为。它不知道有关文本编码的 bean,它始终假定文件中的文本使用默认的系统代码页。几乎不是唯一的问题,读取单个设置时吹出 50 毫秒也很难隐藏。不要使用它。
-
作为
GetPrivateProfileString()方法的替代方案,我编写了一个纯.NET 解决方案,该解决方案利用Regex 来解析INI。您可以从文件或字符串中加载它,从而为您提供指定编码的选项:stackoverflow.com/a/44096742(目前它仅支持读取 INI,但将其调整为写入也不是很复杂) -
@VisualVincent 谢谢。我刚刚在您的答案中添加了一个查询。
标签: vb.net