Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to use ini files

Status
Not open for further replies.

mettodog

Vendor
Jul 11, 2000
94
US
how would i write/read information from an ini file?
 
You should be able to load the file into a rich text box using the loadfile statement.


PK Odendaal
pko@mweb.co.za

 
In a module, put the following code:

Option Explicit

' Read INI file
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
' Write INI file
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 Function sGet_INI_String(rsSection As String, rsEntry As String) As String
' ------------------------
' Retrieve INI file string
' ------------------------

Dim lReturn As Long
Dim sINIString As String
Dim sEntry As String
Dim rsFileName As String

rsFileName = gsINIPATH

' Initialise buffer - safer and quicker
' than fixed length string
sINIString = Space$(255)

' rsEntry must be assigned to local variable to work
sEntry = rsEntry

lReturn = GetPrivateProfileString(rsSection, sEntry, "", sINIString, Len(sINIString), rsFileName)

If lReturn > 0 Then
sGet_INI_String = Left$(sINIString, lReturn)
Else
sGet_INI_String = ""
End If

End Function

Sub Write_INI_String(rsSection As String, rsEntry As String, rsValue As String)
' ---------------------
' Write INI file string
' ---------------------

Dim lReturn As Long
Dim rsFileName As String

rsFileName = gsINIPATH

If rsEntry <> &quot;&quot; Then
lReturn = WritePrivateProfileString(rsSection, rsEntry, rsValue, rsFileName)
Else
' To remove Section
lReturn = WritePrivateProfileString(rsSection, vbNullString, rsValue, rsFileName)
End If
If lReturn = 0 Then
' Error
End If

End Sub


NOTE that these two routines use a global variable - gsINIPATH - which is used to store the path of the ini file.

And then to read from the ini file, do this:

Dim sDatabasePath As String
sDatabasePath = sGet_INI_String(&quot;Database&quot;, &quot;Path&quot;)


And to write to the ini file, do this:

Call Write_INI_String(&quot;Database&quot;, &quot;Path&quot;, &quot;c:\test\data.mdb&quot;)

Your ini file will include a section called Database with an entry called Path, like this:

[Database]
Path=c:\test\data.mdb

Simon
 
You mention that...

NOTE that these two routines use a global variable - gsINIPATH - which is used to store the path of the ini file.

I've done everything but because I'm not sure what you mean by this then it doesn't write and I get an error message. Please could you explain what to do with gsINIPATH so that I can write and read ini.

Thanks...

 
Hi... Sorry, I've solved the problem I mentioned above but could you give me advice about...

I have added the directory path to the ini file which holds the information but what happens when other people install the application and your directory location is still in the module? Does the install procedure rectify this????

Thanks for your help....
 
dodgyone -

In your project code, declare gsINIPATH globally -- at the top of the code and not a part of any component's actions.

Dim gsINIPATH
gsINIPATH = &quot;C:\windows\system.ini&quot;



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top