Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted

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':
VB
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.
 
Share this answer
 
Comments
Shubhendu Bhaskar 7-May-15 14:59pm    
Hey Mario, Like i said on StackOverflow.. we are now using XML inplace of INI files..
But still i would like to complete the portion I left using your Libraries.
I tried downloading your library but in the solution but i cannot find any MadMilkman.Ini.dll.. Is it I need to build the application and use that DLL?
Or you are using manually?
Mario Z 7-May-15 16:14pm    
First I personally also prefer XML over INI format.

Now regarding the DLL, previously I provided a setup and NuGet deployment.
Just now I also added a MadMilkman.Ini.zip, you can download it from here.
Guru35 16-Oct-19 17:53pm    
Excelent library,
thanks you so much
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900