|
-
Aug 7th, 2001, 12:23 AM
#1
Thread Starter
Addicted Member
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
-
Aug 7th, 2001, 12:38 AM
#2
Junior Member
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
-
Aug 7th, 2001, 12:49 AM
#3
-= B u g S l a y e r =-
You can use inifiles :
VB Code:
Option Explicit
Private 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 Long, ByVal lpFileName As String) As _
Long
Private 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
Private Function ReadINI(strsection As String, strkey As String, strfullpath As String) As String
Dim strbuffer As String
Let strbuffer$ = String$(750, Chr$(0&))
Let ReadINI$ = Left$(strbuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strkey$), "", strbuffer, Len(strbuffer), strfullpath$))
End Function
Private Sub WriteINI(strsection As String, strkey As String, strkeyvalue As String, strfullpath As String)
Call WritePrivateProfileString(strsection$, UCase$(strkey$), strkeyvalue$, strfullpath$)
End Sub
Private Sub Command1_Click()
WriteINI "DBParam", "DBName", "C:\myapp\db\mydb.mdb", "C:\TEST.INI"
WriteINI "Temp", "TMPPath", "C:\myapp\tmp", "C:\TEST.INI"
End Sub
Private Sub Command2_Click()
MsgBox ReadINI("DBParam", "DBName", "C:\TEST.INI")
MsgBox ReadINI("Temp", "TMPPath", "C:\TEST.INI")
End Sub
or u could use the registry
VB Code:
Private Sub Command1_Click()
SaveSetting App.EXEName, "Settings", "DBName", "C:\myapp\db\mydb.mdb"
SaveSetting App.EXEName, "Settings", "TMPPath", "C:\myapp\tmp"
End Sub
Private Sub Command2_Click()
MsgBox GetSetting(App.EXEName, "Settings", "DBName", "C:\myapp\db\mydb.mdb")
MsgBox GetSetting(App.EXEName, "Settings", "TMPPath", "C:\myapp\tmp")
End Sub
-
Aug 7th, 2001, 01:09 AM
#4
Thread Starter
Addicted Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|