-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModINIFunctions.vb
75 lines (59 loc) · 2.71 KB
/
ModINIFunctions.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Option Strict On
Module INIAccess
#Region "API Calls"
' standard API declarations for INI access
' changing only "As Long" to "As Int32" (As Integer would work also)
Private Declare Unicode Function WritePrivateProfileString Lib "kernel32" _
Alias "WritePrivateProfileStringW" (ByVal lpApplicationName As String,
ByVal lpKeyName As String, ByVal lpString As String,
ByVal lpFileName As String) As Int32
Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String,
ByVal lpKeyName As String, ByVal lpDefault As String,
ByVal lpReturnedString As String, ByVal nSize As Int32,
ByVal lpFileName As String) As Int32
#End Region
Public Function INIRead(ByVal INIPath As String,
ByVal SectionName As String, ByVal KeyName As String,
ByVal DefaultValue As String) As String
' primary version of call gets single value given all parameters
Dim n As Int32
Dim sData As String
sData = Space$(1024) ' allocate some room
n = GetPrivateProfileString(SectionName, KeyName, DefaultValue,
sData, sData.Length, INIPath)
If n > 0 Then ' return whatever it gave us
INIRead = sData.Substring(0, n)
Else
INIRead = ""
End If
End Function
#Region "INIRead Overloads"
Public Function INIRead(ByVal INIPath As String,
ByVal SectionName As String, ByVal KeyName As String) As String
' overload 1 assumes zero-length default
Return INIRead(INIPath, SectionName, KeyName, "")
End Function
Public Function INIRead(ByVal INIPath As String,
ByVal SectionName As String) As String
' overload 2 returns all keys in a given section of the given file
Return INIRead(INIPath, SectionName, Nothing, "")
End Function
Public Function INIRead(ByVal INIPath As String) As String
' overload 3 returns all section names given just path
Return INIRead(INIPath, Nothing, Nothing, "")
End Function
#End Region
Public Sub INIWrite(ByVal INIPath As String, ByVal SectionName As String,
ByVal KeyName As String, ByVal TheValue As String)
Call WritePrivateProfileString(SectionName, KeyName, TheValue, INIPath)
End Sub
Public Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String,
ByVal KeyName As String) ' delete single line from section
Call WritePrivateProfileString(SectionName, KeyName, Nothing, INIPath)
End Sub
Public Sub INIDelete(ByVal INIPath As String, ByVal SectionName As String)
' delete section from INI file
Call WritePrivateProfileString(SectionName, Nothing, Nothing, INIPath)
End Sub
End Module