GetPrivateProfileString:Strange Behavior,仅在文件被排除并再次包含在项目中时才有效 [英] GetPrivateProfileString: Strange Behaviour, works only when the file is excluded and included again in project

查看:60
本文介绍了GetPrivateProfileString:Strange Behavior,仅在文件被排除并再次包含在项目中时才有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从VB6到VB.NET 4.0(VS 2013 Professional)的迁移项目,我应该使用INI文件来读取输入(客户端要求,只需要使用下面的方法)。



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



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

< DllImport(kernel32)> _
公共共享函数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
结束函数





其中m_sIniFileName的类型为String。



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



知道这里发生了什么问题吗?

解决方案

注意两个 GetPrivateProfileString WritePrivateProfileString Win API非常错误,很久以前就被弃用了。

您看到这些功能只提供与16位应用程序的兼容性,我我相当肯定你没有发展所以你应该重新考虑使用它。



除了这些错误你必须意识到每次阅读和/或写作你都在访问当你做一些实际数量的读数和/或着作时,文件会产生可怕的表现。



我建议你改用我的VB.NET INI文件库 [ ^ ]。例如,以下是如何退出指定'section'及其'key'的值:

  Dim  ini 作为  IniFile()
ini.Load(m_sIniFileName)

Dim sReturnValue 作为 字符串 = ini.Sections(section).Keys(key).Value

' 或检索值为整数。
' 旁注TryParseValue返回boolean,指示解析是否成功。
Dim iReturnValue 作为 整数
ini.Sections(section).Keys(key).TryParseValue(iReturnValue)



如果您需要任何帮助,请告诉我。

虽然这项技术非常原始,根本不推荐,但只是回答这个问题,我工作并找到了实际问题。 

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

有关更多信息,请参阅http://www.interworks.com/blogs/dsmith/2011/09/21/disabling-windows-7-virtual-store。

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

一旦完全访问权限授予该软件,它的工作原理。

强烈建议不要使用此解决方案,请使用其他方法来实现相同的功能


I am working on a migration project from VB6 to VB.NET 4.0 (VS 2013 Professional) where I am supposed to use INI file for reading inputs (client requirements , need to use this below method only).

To read a specific file path, code calls the function to read INI file. The declaration is as:

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



where m_sIniFileName is of type String.

The strange thing is, when I freshly open the solution, no value is returned from "GetPrivateProfileString", but when I exclude and then include the Class file (which includes this code), the expected value is returned and it works fine till the time I close and open the solution again.

Any idea what wrong is going on here?

解决方案

Note that both GetPrivateProfileString and WritePrivateProfileString Win APIs are very buggy and have been deprecated long time ago.
You see these functions where provided only for compatibility with 16-bit applications, which I'm fairly certain that you are not developing so you should really reconsider using that.

Beside the bugs you must realize that on each reading and/or writing you are accessing the file and that will just yield terrible performances when you do somewhat realistic number of readings and/or writings.

May I suggest you to instead use my INI file library for VB.NET[^]. For example here is how you can retireve the value of specified 'section' and its 'key':

Dim ini As New IniFile()
ini.Load(m_sIniFileName)

Dim sReturnValue As String = ini.Sections(section).Keys(key).Value

' Or retrieve the value as integer.
' Side note TryParseValue returns boolean that indicates if the parsing succeeded.
Dim iReturnValue As Integer
ini.Sections(section).Keys(key).TryParseValue(iReturnValue)


Let me know if you need any help with this.


Though this technology is quite primitive and not recommended at all, but just to answer the question, I worked and found what the actual problem was.

The issue was with "m_sIniFileName". The Software which was changing the IniFiles did not have privelges to actually modify the C:\WINDOWS\Win.Ini (files from where we are reading the values) , instead the values were getting updated in Virtual Store, C:\Users(User)\AppData\Local\VirtualStore\Windows\win.ini.

Please refer to the http://www.interworks.com/blogs/dsmith/2011/09/21/disabling-windows-7-virtual-store  for more information.

So, users has to grant the permission to the third party software to modify C:\WINDOWS\Win.Ini (not recommended)

Once the full access is granted to the software, it worked.

This solution is highly NOT recommended, please use another methods to achieve the same functionality


这篇关于GetPrivateProfileString:Strange Behavior,仅在文件被排除并再次包含在项目中时才有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆