2 Command ekleyin form'a kaydet - yükle gibisinden
Yükle Butonuna:
check1.Value = ReadIniValue(App.Path & "\dosya adı.ini", "Default", "başlat")
Kaydettirme:
WriteIniValue App.Path & "\dosya adı.ini", "Default", "başlat", text1.Text
Arkadaşlar size örnek ini dosyasına yazılacakları veriyorum:
[Default]
Başlat=1
1 = başlatır
0 = durdurur
Modül Kodlamamız:
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
Declare Function GetPrivateProfileString Lib "kernel32.dll" _
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
Public Function WriteIniValue(INIpath As String, PutKey As String, PutVariable As String, PutValue As String)
On Error Resume Next
Dim Temp As String
Dim LcaseTemp As String
Dim ReadKey As String
Dim ReadVariable As String
Dim LOKEY As Integer
Dim HIKEY As Integer
Dim KEYLEN As Integer
Dim VAR As Integer
Dim VARENDOFLINE As Integer
Dim NF As Integer
Dim x As Integer
AssignVariables:
NF = FreeFile
ReadKey = vbCrLf & "[" & LCase$(PutKey) & "]" & Chr$(13)
KEYLEN = Len(ReadKey)
ReadVariable = Chr$(10) & LCase$(PutVariable) & "="
EnsureFileExists:
Open INIpath For Binary As NF
Close NF
SetAttr INIpath, vbArchive
LoadFile:
Open INIpath For Input As NF
Temp = Input$(LOF(NF), NF)
Temp = vbCrLf & Temp & "[]"
Close NF
LcaseTemp = LCase$(Temp)
LogicMenu:
LOKEY = InStr(LcaseTemp, ReadKey)
If LOKEY = 0 Then GoTo AddKey:
HIKEY = InStr(LOKEY + KEYLEN, LcaseTemp, "[")
VAR = InStr(LOKEY, LcaseTemp, ReadVariable)
If VAR > HIKEY Or VAR < LOKEY Then GoTo AddVariable:
GoTo RenewVariable:
AddKey:
Temp = Left$(Temp, Len(Temp) - 2)
Temp = Temp & vbCrLf & vbCrLf & "[" & PutKey & "]" & vbCrLf & PutVariable & "=" & PutValue
GoTo TrimFinalString:
AddVariable:
Temp = Left$(Temp, Len(Temp) - 2)
Temp = Left$(Temp, LOKEY + KEYLEN) & PutVariable & "=" & PutValue & vbCrLf & Mid$(Temp, LOKEY + KEYLEN + 1)
GoTo TrimFinalString:
RenewVariable:
Temp = Left$(Temp, Len(Temp) - 2)
VARENDOFLINE = InStr(VAR, Temp, Chr$(13))
Temp = Left$(Temp, VAR) & PutVariable & "=" & PutValue & Mid$(Temp, VARENDOFLINE)
GoTo TrimFinalString:
TrimFinalString:
Temp = Mid$(Temp, 2)
Do Until InStr(Temp, vbCrLf & vbCrLf & vbCrLf) = 0
Temp = Replace(Temp, vbCrLf & vbCrLf & vbCrLf, vbCrLf & vbCrLf)
Loop
Do Until Right$(Temp, 1) > Chr$(13)
Temp = Left$(Temp, Len(Temp) - 1)
Loop
Do Until Left$(Temp, 1) > Chr$(13)
Temp = Mid$(Temp, 2)
Loop
OutputAmendedINIFile:
Open INIpath For Output As NF
Print #NF, Temp
Close NF
End Function
Public Function ReadIniValue(INIpath As String, KEY As String, Variable As String) As String
On Error Resume Next
Dim NF As Integer
Dim Temp As String
Dim LcaseTemp As String
Dim ReadyToRead As Boolean
AssignVariables:
NF = FreeFile
ReadIniValue = ""
KEY = "[" & LCase$(KEY) & "]"
Variable = LCase$(Variable)
EnsureFileExists:
Open INIpath For Binary As NF
Close NF
SetAttr INIpath, vbArchive
LoadFile:
Open INIpath For Input As NF
While Not EOF(NF)
Line Input #NF, Temp
LcaseTemp = LCase$(Temp)
If InStr(LcaseTemp, "[") <> 0 Then ReadyToRead = False
If LcaseTemp = KEY Then ReadyToRead = True
If InStr(LcaseTemp, "[") = 0 And ReadyToRead = True Then
If InStr(LcaseTemp, Variable & "=") = 1 Then
ReadIniValue = Mid$(Temp, 1 + Len(Variable & "="))
Close NF: Exit Function
End If
End If
Wend
Close NF
End Function
Function ReadINI(section As String, KeyName As String, FileName As String) As String
Dim sRet As String
sRet = String(255, Chr(0))
ReadINI = Left(sRet, GetPrivateProfileString(section, ByVal KeyName$, "", sRet, Len(sRet), FileName))
End Function
Function WriteINI(sSection As String, sKeyName As String, sNewString As String, sFileName) As Integer
Dim r
r = WritePrivateProfileString(sSection, sKeyName, sNewString, sFileName)
End Function
INI Sistemi ne işe yarar:
- Oyun yapımcılarına
- Programın komutlarını tamamen kullanıcıya bırakmaya (program adı değiştirme vs.)
- Açık kaynak sunmaya.
- Ve dahası...
Ek Olarak: bir örnek daha veriyorum:
dim title
form1.caption = title
command1.Value = ReadIniValue(App.Path & "\dosya adı.ini", "Default", "title")
[Default]
title=wmaraci.com
:)