[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

Create .ini file to pull and put settings to.

How can I create a .ini file to save program settings to and retrieve the next time the program is loaded?
0
q_ship
Asked:
q_ship
1 Solution
 
CraigLazarCommented:
Hi
here is some code thath does the trick
Private Sub Command1_Click()
       CommonDialog1.Action = 1
       PathField.Text = CommonDialog1.filename
       Dim path As String
       Dim PathName As String
       PathName = PathField.Text
       Open "c:\tflorist.InI" For Output Shared As #1
       Write #1, PathName    ' Write comma-delimited data.
       Write #1,   ' Write blank line.
       Close #1
       Unload Registry
End Sub

this function pops up a form for the user to browse and select the database file. It then auto creates an ini file called tflorist.ini with the path that the user selected

'********  to read from the ini **********
 Public Function databasePath()
On Error GoTo error_handler:
        Open "c:\Tflorist.InI" For Input Shared As #1
        Input #1, TempPath
        Close #1

error_handler:
If Err.Number = 53 Then
       MsgBox "Please can you use the next screen to Locate the Database file on the Network Server", vbInformation
       Load Registry
       Registry.Show 1
      Resume
       End If
End Function

This function opens and reads the ini file for it's value

Now if you sre going to have multiple values in an ini file you need to make calls to the Api
getprivateprofile String
writeprivateprofile string

'Declare statements for the Dll's which go in the General Declarations Section of the form
'\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
Private Declare Function WritePrivateProfileStringA Lib "kernel32.dll" (ByVal ipszsection As String, ByVal ipszkey As String, ByVal ipszString As String, ByVal ipszfile As String) As Long

Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _

Dim wTest As Long
Dim wAppName As String
Dim wKeyName As String
Dim wString As String
Dim wFile As String
Dim rAppName As String
Dim rKeyName As String
Dim rDefault As String
Dim rReturnedStrings As String
Dim rSize As Long
Dim rFile As String
Dim rTest As Long
Dim Tempbuff As Variant

Call DatPath
Dim teste As String
dbpath = dbpat
rAppName = "Drivers"
rKeyName = "bpsdriver"
rDefault = "not"
rReturnedStrings = Tempbuff
rSize = 20
rFile = "c:\windows\system.ini"
Tempbuff = GetPrivateProfileString(rAppName, rKeyName, rDefault, rReturnedStrings, rSize, rFile)
If Tempbuff <> 10 Then
    wAppName = "Drivers"
    wKeyName = "Bpsdriver"
    wString = "bpsdrv.dll"
    wFile = "system.ini"
    wTest = WritePrivateProfileStringA(wAppName, wKeyName, wString, wFile)
End If


good luck
Craig
0
 
mdouganCommented:
Just a note, GetPrivateProfileString has a place where you can specify "default" values - so, in the procedure where you call it, if GetPrivateProfileString does not find your INI file, then it will return the defaults you provide - so you can populate your program variables.  

Then, in your application, when you close down, you should WritePrivateProfileString your program variables to the INI file.  If the INI file does not exist (as it wont the first time you run), then it will create it for you with the proper Section names etc.

The next time you run your program, then GetPrivateProfileString should be able to find your INI file.

0
 
q_shipAuthor Commented:
Thank you for the code, that points me in the right direction!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now