Visual Basic Codes
ActiveX
Miscellaneous
Applications
Code Snippets
Common Dialogs
Special Effects
Database Stuff
Date Time
Files Drives
Forms
Graphics Games
Internet Stuff
Multimedia
Other
Strings
Windows
Visual Basic > Files Directories Drives sample source codes
Read and write to an INI file.
Read and write to an INI file. 'How do I write and read to an INI file? Function ReadWriteINI(Mode As String, tmpSecname As String, tmpKeyname As String, Optional tmpKeyValue) As String Dim tmpString As String On Error GoTo ReadWriteINIError ' ' Mode = "WRITE" or "GET" ' 'Here are the declare functions '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 '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 ' *** set the return value to OK ReadWriteINI = "OK" ' *** test for good data to work with If IsNull(Mode) Or Len(Mode) = 0 Then ReadWriteINI = "ERROR MODE" ' Set the return value Exit Function End If If IsNull(tmpSecname) Or Len(tmpSecname) = 0 Then ReadWriteINI = "ERROR Secname" ' Set the return value Exit Function End If If IsNull(tmpKeyname) Or Len(tmpKeyname) = 0 Then ReadWriteINI = "ERROR Keyname" ' Set the return value Exit Function End If ' *** set the ini file name filename = "C:\Vbasic\Test\WinPlace.ini" ' <<<<< put your file name here ' ' ' ******* WRITE MODE ************************************* If UCase(Mode)="WRITE" Then If IsNull(tmpKeyValue) Or Len(tmpKeyValue)="0" Then ReadWriteINI="ERROR KeyValue" Exit Function Else secname="tmpSecname" keyname="tmpKeyname" keyvalue="tmpKeyValue" anInt="WritePrivateProfileString(secname," keyname, keyvalue, filename) End If End If ' ******************************************************* ' ' ******* GET MODE ************************************* If UCase(Mode)="GET" Then secname="tmpSecname" keyname="tmpKeyname" defaultkey="Failed" keyvalue="String$(50," 32) anInt="GetPrivateProfileString(secname," keyname, defaultkey, keyvalue, Len(keyvalue), filename) If Left(keyvalue, 6) <> "Failed" Then ' *** got it tmpString = keyvalue tmpString = RTrim(tmpString) tmpString = Left(tmpString, Len(tmpString) - 1) End If ReadWriteINI = tmpString End If Exit Function ' ******* ReadWriteINIError: MsgBox Error Stop End Function
Privacy Policy
|
Link to Us
|
Links