Read/Write to .ini files in vb.net
I'm trying to read and write .ini files from a VB.NET DLL. I've copied my VB6 code (declarations for GetPrivateProfileString, WritePrivateProfileString, etc.) and they don't appear to work. Instead of getting the contents of my ini file I get a 0 and my returned number of bytes is 5324320632995841.
Does anybody have any ideas? Do I need to change my API calls to work under .NET?
Cheers
Re: Read/Write to .ini files in vb.net
I have copied and pasted this code into by VB.NET project, and .NET does not seem to have a problem with the code, however nothing is being written to my INI file. I have created the INI file by using a File.Create("ExportSettings.ini"), which creates a file in the same directory as my executable, but when I try to write values to the INI file, nothing shows up. Here is my code:
Dim sINIpath as String
sINIpath = "ExportSettings.ini"
If File.Exists(sINIpath) = False then
File.Create(sINIpath)
modINIAccess.INIWrite(sINIpath, "sde", "connectstring", "1")
End If
Any insight would be greatly appreciated!
Re: Read/Write to .ini files in vb.net
Re: Read/Write to .ini files in vb.net
Well I notice that you are not specifying the full path to the file... you have no directory listed, just the filename...
Code:
sINIpath = "ExportSettings.ini"
That needs to be the full path... if you are trying to read or write it to the application folder, change it to
Code:
sINIpath = Application.StartupPath & "\ExportSettings.ini"
Re: Read/Write to .ini files in vb.net
There is a free library called Nini that I use to read and write ini files (and other configuration formats also):
http://nini.sourceforge.net/
It's great. It provides a unified interface to all configuration formats (ini, xml, command line arguments and registry). No need to use those ugly API commands.
Re: Read/Write to .ini files in vb.net
Of course, if you want to get very basic with this, you just create a simple text file, each line you specify a name, a seperator, and a value... then just use a streamreader, reading each line in, splitting on the seperator, checking the name, if it is the name you want, then use its value...