Results 1 to 5 of 5

Thread: Reading and Writing to INI

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 1999
    Location
    Milford, NH
    Posts
    15

    Post

    Whats the comand to read an INI file?
    Whats the command to write to INI file?

    Thanks for you help.

  2. #2
    Junior Member
    Join Date
    Feb 2000
    Location
    England
    Posts
    26

    Post

    Add the MS Scripting Runtime component

    Here's the code to open the file and then write a line of code to the bottom of it.

    Option Explicit

    Dim oFso As New FileSystemObject
    Dim oFile As TextStream
    Private Sub Form_Load()
    oFso.OpenTextFile ("C:\your file.INI"), ForReading ' open the ini file
    oFile.ReadAll ' read all the ini file
    oFile.Close
    End Sub

    Sub Addline()
    oFso.OpenTextFile ("C:\your file.ini"), ForAppending
    oFile.WriteLine ("C:\your new line here")
    oFile.Close
    End Sub

  3. #3
    New Member
    Join Date
    Feb 2000
    Location
    Hull,Quebec, Canada
    Posts
    7

    Post

    That method will work, but the much easier method is to use the following API's:

    ** Getting Private ini (your application ini)

    Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long

    Public Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, 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

    **Writing your private ini

    Public Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long

    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

    ** getting public ini (win.ini file)

    Public Declare Function GetProfileInt Lib "kernel32" Alias "GetProfileIntA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Long) As Long

    Public Declare Function GetProfileSection Lib "kernel32" Alias "GetProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

    Public Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long

    ** writing public ini (win.ini


    Public Declare Function WriteProfileSection Lib "kernel32" Alias "WriteProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String) As Long

    Public Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long



  4. #4
    Lively Member Xero's Avatar
    Join Date
    Feb 2000
    Posts
    75

    Post

    In my app, I used some of the source from VBWorld, and some of my own to make it easier on myself... here it is...

    Public Function IniString(Heading As String, Key As String, file As String) As String
    Dim ret As Long
    Dim Temp As String * 50

    ret = GetPrivateProfileString(Heading, Key, App.Path & "\" & file, Temp, Len(Temp), App.Path & "\" & file)

    If ret = 0 Then
    Beep
    Else
    IniString = Trim(Temp)
    End If
    End Function

    -------
    So, for an example, say you wanted what data was in the .ini file wowyzowy.ini under section Wow and string Zowy.

    Dim Value as string
    Value = Inistring("Wow", "Zowy", "wowyzowy.ini")
    MsgBox Value

    ----
    This would popup a message box with the value in that. Here's how wowyzowy.ini should be structured:

    [Wowy]
    Zowy=Boo!

    So, the code above would display a message box saying Boo!. Hehe... k... Thats to read, to write simply do the same thing, but with an added value peramiter...


    Public Sub StoreIni(Heading As String, Key As String, Value, file As String)
    Dim ret As Long
    ret = WritePrivateProfileString(Heading, Key, Value, App.Path & "\" & file)

    If ret = 0 Then
    Beep
    End If
    End Sub

    This would work the same way, just use something like...

    StoreIni "Wowy", "Zowyzoo", "You're not all that and a bag of potato chips", "wowyzowy.ini"

    And the ini file would now look like....

    [Wowy]
    Zowyzoo=You're not all that and a bag of potato chips

    Ok, hope i helped you out! Oh yes, it will beep using the computers speaker if something is wrong.

    -Xero

    Edited by Xero on 02-25-2000 at 07:25 PM

  5. #5
    Junior Member
    Join Date
    Jul 2000
    Location
    Antwerp,Belgium
    Posts
    21

    Question Windows 2000

    Hi,

    I'm using the WriteProfileString api to set the default printer from code.

    This works fine in WinNT but not in Windows 2000.

    Does anyone know which api call I should use?

    Thanks

    God and coincidence are two big humorists

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width