Last active
February 2, 2019 22:21
-
-
Save ooltcloud/034069ca49201531b9d3 to your computer and use it in GitHub Desktop.
[VB.NET] ini ファイル操作クラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class IniFileAccessor | |
''' <summary> | |
''' iniファイル名 | |
''' </summary> | |
Private _iniFileName As String | |
''' <summary> | |
''' 読込用テンポラリ領域 | |
''' </summary> | |
Private _tempValue As String | |
Private Declare Function GetPrivateProfileString Lib "KERNEL32.DLL" Alias "GetPrivateProfileStringA" _ | |
( | |
sectionName As String, | |
keyName As String, | |
defaultValue As String, | |
value As String, | |
size As Integer, | |
fileName As String | |
) As Boolean | |
Private Declare Function WritePrivateProfileString Lib "KERNEL32.DLL" Alias "WritePrivateProfileStringA" _ | |
( | |
sectionName As String, | |
keyName As String, | |
value As String, | |
fileName As String | |
) As Boolean | |
Sub New(iniFileName As String) | |
_iniFileName = iniFileName | |
_tempValue = New String(vbNullChar, 32768) | |
End Sub | |
Public Function GetString(sectionName As String, keyName As String, Optional defaultValue As String = "") As String | |
Dim ret = GetPrivateProfileString(sectionName, keyName, defaultValue, _tempValue, _tempValue.Length, _iniFileName) | |
If ret = False Then | |
Throw New ApplicationException("IniFile の読み込みに失敗しました") | |
End If | |
Dim value = _tempValue.Substring(0, _tempValue.IndexOf(vbNullChar)) | |
Return value | |
End Function | |
Public Sub SetString(sectionName As String, keyName As String, value As String) | |
Dim ret = WritePrivateProfileString(sectionName, keyName, value, _iniFileName) | |
If ret = False Then | |
Throw New ApplicationException("IniFile の書き込みに失敗しました") | |
End If | |
End Sub | |
End Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you.
It was very helpful.