Results 1 to 4 of 4

Thread: using external file to provide parameters

  1. #1

    Thread Starter
    Addicted Member chrisvl's Avatar
    Join Date
    Jul 2001
    Location
    Belgium - Somewhere between the bedroom and the toilet...or at work playing Oracle DBA :-)
    Posts
    233

    using external file to provide parameters

    Hello,

    I want to use an external file to provide parameters for my application.
    This could be a simple text file with two lines in it:
    one to provide the path and name of my database and one to specify a temporary directory.

    How do I use such a file? Is it possible to use constants or global variables and have them filled up with the data from the text file when the application starts?

    How do I do that. And how / where must I specify the name and path of that parameter file?

    Please help!


    Thanks


    Christophe

  2. #2
    Junior Member
    Join Date
    Sep 2000
    Location
    Norway
    Posts
    30

    Use ini-file!

    You have to declare the following:

    Private Declare Function GetPrivateProfileString% Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpAppName As String, ByVal lpkeyname As Any, ByVal lpDefault As String, ByVal lpReturnString As String, ByVal nSize As Integer, ByVal lpFilename As String)

    Public s_Database as string
    Public s_Directory as string

    Private Sub Form_Load()

    s_Database = LoadIniSettings ("CONFIG", "DATABASE")
    S_Directory = LoadIniSettings("CONFIG", "DIRECTORY")

    End Sub

    Function LoadIniSettings(Section As String, FieldName As String) As String
    lpFileName$ = "MYSETTINGS.INI"
    lpApplicationname$ = Section
    lpKeyName$ = FieldName
    lpdefault$ = "NotFound"
    lpReturnedstring$ = Space$(300)
    nSize = 300
    result% = GetPrivateProfileString%(lpApplicationname$, lpKeyName$, lpdefault$, lpReturnedstring$, nSize, lpFileName$)


    LoadIniSettings = Left$(lpReturnedstring$, InStr(lpReturnedstring$, Chr$(0)) - 1)

    End Function


    ************

    The ini-file must look like this
    [CONFIG]
    DATABASE=C:\MYDB.MDB
    DIRECTOY=C:\MYDIRECTORY


    Hope this helps


    o8<

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    You can use inifiles :
    VB Code:
    1. Option Explicit
    2. Private Declare Function GetPrivateProfileString _
    3. Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal _
    4. lpApplicationName As String, ByVal lpKeyName As String, _
    5. ByVal lpDefault As String, ByVal lpReturnedString As _
    6. String, ByVal nSize As Long, ByVal lpFileName As String) As _
    7. Long
    8.  
    9. Private Declare Function WritePrivateProfileString _
    10. Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal _
    11. lpApplicationName As String, ByVal lpKeyName As Any, ByVal _
    12. lpString As Any, ByVal lpFileName As String) As Long
    13.  
    14.  
    15.  
    16. Private Function ReadINI(strsection As String, strkey As String, strfullpath As String) As String
    17.    Dim strbuffer As String
    18.    Let strbuffer$ = String$(750, Chr$(0&))
    19.    Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
    20. End Function
    21.  
    22. Private Sub WriteINI(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
    23.     Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
    24. End Sub
    25.  
    26. Private Sub Command1_Click()
    27.     WriteINI "DBParam", "DBName", "C:\myapp\db\mydb.mdb", "C:\TEST.INI"
    28.     WriteINI "Temp", "TMPPath", "C:\myapp\tmp", "C:\TEST.INI"
    29.    
    30. End Sub
    31.  
    32. Private Sub Command2_Click()
    33.     MsgBox ReadINI("DBParam", "DBName", "C:\TEST.INI")
    34.     MsgBox ReadINI("Temp", "TMPPath", "C:\TEST.INI")
    35. End Sub

    or u could use the registry

    VB Code:
    1. Private Sub Command1_Click()
    2.     SaveSetting App.EXEName, "Settings", "DBName", "C:\myapp\db\mydb.mdb"
    3.     SaveSetting App.EXEName, "Settings", "TMPPath", "C:\myapp\tmp"
    4. End Sub
    5.  
    6. Private Sub Command2_Click()
    7.     MsgBox GetSetting(App.EXEName, "Settings", "DBName", "C:\myapp\db\mydb.mdb")
    8.     MsgBox GetSetting(App.EXEName, "Settings", "TMPPath", "C:\myapp\tmp")
    9. End Sub
    -= a peet post =-

  4. #4

    Thread Starter
    Addicted Member chrisvl's Avatar
    Join Date
    Jul 2001
    Location
    Belgium - Somewhere between the bedroom and the toilet...or at work playing Oracle DBA :-)
    Posts
    233
    Ok,

    Thanks to both of you !
    I succeeded in reading the contents of the ini-file !

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