Reading / Writing an INI file 
Author Message
 Reading / Writing an INI file

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?

Regards,
Michael.



Sun, 29 Aug 2004 17:04:07 GMT  
 Reading / Writing an INI file
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?

Regards,
Michael.



Sun, 29 Aug 2004 17:18:44 GMT  
 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.



Sun, 29 Aug 2004 20:21:58 GMT  
 Reading / Writing an INI file
This is not my example and was kindly posted by another newsgroup
contributor, it might help you work out your own code for this.

--
Regards

John Timney (Microsoft ASP.NET MVP)
----------------------------------------------
<shameless_author_plug>
Professional Windows Forms
     ISBN: 1861005547
Professional JSP
    ISBN: 1861003625
Beginning JSP Web Development
    ISBN: 1861002092
</shameless_author_plug>
----------------------------------------------

 Given the following INI file (called ini1.ini):

 ---------8<------- cut here ---------------
 [Section1]
 KeyName=The key value
 ---------8<------- cut here ---------------

 You can use the following code to read that value:

 ---------8<------- cut here ---------------

 ---------8<------- cut here ---------------
 using System;
 using System.Text;
 using System.Runtime.InteropServices;

 namespace IniFileTester
 {
     class Class1
     {
         [DllImport("kernel32.dll", SetLastError=true,
 CharSet=System.Runtime.InteropServices.CharSet.Auto)]
         internal unsafe static extern int GetPrivateProfileString(String
 lpAppName, String lpKeyName, String lpDefault,
             StringBuilder lpReturnedString, int nSize, String lpFileName);

         static void Main(string[] args)
         {
             String sectionName = "Section1";
             String keyName = "KeyName";
             String defaultValue = "Default";
             String iniFilename =

             String retVal;
             retVal = GetIniString(sectionName, keyName, defaultValue,
 iniFilename);
             Console.WriteLine(retVal);
         }

        public unsafe static String GetIniString(String sectionName,
String
 keyName, String defaultValue, String iniFileName)
         {
             StringBuilder sb = new StringBuilder(255);
            int retVal;
             retVal = GetPrivateProfileString(sectionName, keyName,
 defaultValue, sb, 255, iniFileName);
            return sb.ToString();
         }
    }
 }



Mon, 30 Aug 2004 15:26:29 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. read/write an .ini file

2. Reading / Writing an INI file

3. How to Read/Write to INI FIle

4. Read/Write to INI files in VB5

5. Read/Write to INI files in VB5???

6. How to read/write to ini file on a network?

7. Read/Write INI Files w/Access 7.0

8. Ini-file, read write and empty lines.

9. Reading and writing from and to ini files

10. Reading and writing to an INI file?

11. Reading and Writing to .ini files - help please.

12. Read and Write to INI files

 

 
Powered by phpBB® Forum Software