GetPrivateProfileString:奇怪的行为,仅当文件被排除并再次包含在项目中时才有效

GetPrivateProfileString: Strange Behaviour, works only when the file is excluded and included again in project

我正在进行从 VB6 到 VB.NET 4.0 (VS 2013 Professional) 的迁移项目,我应该在其中使用 INI 文件读取输入(客户要求)。

要读取特定的文件路径,代码调用函数读取INI文件。声明如下:

Dim i As Integer = IniFile.GetPrivateProfileString(Section, Key, "", temp, 1024, m_sIniFileName)


 <DllImport("kernel32")> _
Public Shared Function GetPrivateProfileString(ByVal section As String, ByVal key As String, ByVal def As String, ByVal retVal As System.Text.StringBuilder, ByVal size As Integer, ByVal filePath As String) As Integer
End Function

其中 m_sIniFileName 是字符串类型。

奇怪的是,当我刚打开解决方案时,"GetPrivateProfileString" 没有返回任何值,但是当我排除然后包含 Class 文件(其中包含此代码)时,返回预期值并且它工作正常,直到我关闭并再次打开解决方案。

知道这里出了什么问题吗?

根据 David Heffernan 的建议,我们同意停止使用 GetPrivateProfileString,我们现在使用 XML 来解决这个问题。

虽然这项技术很原始,根本不推荐,但为了回答问题,我工作了一下,发现了实际问题所在。

问题出在 "m_sIniFileName"。 正在更改 IniFiles 的软件没有实际修改 C:\WINDOWS\Win.Ini(我们正在读取值的文件)的权限,而是在 Virtual Store 中更新值, C:\Users(用户)\AppData\Local\VirtualStore\Windows\win.ini.

更多信息请参考link

因此,用户必须授权第三方软件修改C:\WINDOWS\Win.ini(不推荐)

一旦授予该软件的完全访问权限,它就可以运行了。

强烈不推荐此解决方案,请使用其他方法实现相同的功能