
Reading / Writing an INI file
Quote:
> I have code to do this in VB6 using an API call, GetPrivateProfileString,
> but it does not work in VB.NET. Can anyone please tell me how to read /
> write an INI file?
You can still use the API calls. You just need to modify them slightly. If
you were using the API add-in for VB6, the declarations for reading /
writing to an INI file would look something like this:
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias
"WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias
"GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
ByVal lpDefault As String, ByVal lpReturnedString As String, _
ByVal nSize As Long, ByVal lpFileName As String) As Long
The first thing you need to do is change all your long variables to
integers. Then, change your "as any" declarations to "as string". Your new
declarations would look like this:
Public 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
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias
"GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName As String, _
ByVal lpDefault As String, ByVal lpReturnedString As String, _
ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Once these are set up, just call them like you normally would. Be sure to
initialize your return buffer properly to avoid any unexpected errors.
--
Scott Hembrough
Note: Remove the ns. from my address when responding via e-mail.