Click here to Skip to main content
15,886,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have some inherited legacy code that reads and writes from ini file. We recently upgraded to Server 2008 (I know we are behind the times) but writing quit working.

Declare Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName _
As String, ByVal lpKeyName As String, ByVal lpString As String, _
ByVal lpFileName As String) As Integer

This works for reading it.

GetPrivateProfileString(sSection, sKey, sDefault, sTemp, 511, sINIFile)

Any suggestions before I turn it into an xml file?

Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 9-Mar-15 20:00pm    
Suggestions for what? Any problems?
—SA
Member 10198178 10-Mar-15 9:46am    
Yes the Write does not work, It does not throw any errors. The file is just never updated
Mario Z 11-Mar-15 4:52am    
Unfortunately I would need more information, maybe even a sample code of your executable that would reproduce your issue.

Nevertheless I understand that this is a legacy code so you probably don't have a choice but to use INI files, but I need to point out that you are using a deprecated Win32 API functions, they are buggy and they will not be fixed.
So I would suggest you to use some alternative approach for working with INI file.

Again if I may suggest you to try the library that I linked at the end of my answer, it has an intuitive and .NET friendly interface which will enable you to process INI files with ease.
Also you can find VB.NET sample codes that will quickly get you started.

1 solution

Hi, I'm not sure if I understood you correctly but if you are looking for an alternative to INI file approach then how about web.config files?
Why, Where, and How of .NET Configuration Files

Also just in case you are interested here is an example how you can convert that legacy INI into an XML:
VB
Dim iniFile As New IniFile()
iniFile.Load("Sample.ini")

Using xmlFile = XmlWriter.Create("Sample.xml")

	xmlFile.WriteStartDocument()
	xmlFile.WriteStartElement("sections")

	For Each iniSection In iniFile.Sections

		xmlFile.WriteStartElement("section")
		xmlFile.WriteAttributeString("name", iniSection.Name)

		For Each iniKey In iniSection.Keys

			xmlFile.WriteStartElement("key")
			xmlFile.WriteAttributeString("name", iniKey.Name)
			xmlFile.WriteString(iniKey.Value)
			xmlFile.WriteEndElement()

		Next
		xmlFile.WriteEndElement()
	Next
	xmlFile.WriteEndDocument()
End Using

Above code uses an API from my IniFile library for .NET.
 
Share this answer
 

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