Skip to content

Instantly share code, notes, and snippets.

@ooltcloud
Last active February 2, 2019 22:21
Show Gist options
  • Save ooltcloud/034069ca49201531b9d3 to your computer and use it in GitHub Desktop.
Save ooltcloud/034069ca49201531b9d3 to your computer and use it in GitHub Desktop.
[VB.NET] ini ファイル操作クラス
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
@ShogoKoyama
Copy link

Thank you.
It was very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment