サンプル
コードのダウンロード (vbmigtips_INIfile.exe, 114
KB)
アプリケーションの情報データを保存する場合は、通常 INI
ファイル、構成ファイルまたは、レジストリを使用することで実現します。今回は、INI
ファイルと構成ファイルを使用してデータの書き込みと読み込みを行う方法について紹介します。
INI
ファイル.NET Framework クラスライブラリには、INI
ファイルにアクセスするためのクラスは存在していません。そのため、Visual Basic .NET で INI
ファイルへのアクセスを実現するためには、Win32 API を呼び出す必要があります。
INI
ファイルにデータを書き込む場合は、WritePrivateProfileString 関数を使用します。INI
ファイルから、指定したセッションやキーのデータを読み込む場合は GetPrivateProfileString 関数や
GetPrivateProfileInt 関数を使用します。
まず、Win32 API
の関数の定義を宣言します。今回は、String 型のデータと Integer 型のデータを読み書きします。
<DllImport("KERNEL32.DLL")>
_
Public Shared Function
WritePrivateProfileString( _
ByVal lpAppName
As String, _
ByVal lpKeyName
As String, _
ByVal lpString
As String, _
ByVal
lpFileName As String) As Integer
End Function
<DllImport("KERNEL32.DLL",
CharSet:=CharSet.Auto)> _
Public Shared Function
GetPrivateProfileString( _
ByVal lpAppName
As String, _
ByVal lpKeyName
As String, ByVal lpDefault As String, _
ByVal
lpReturnedString As System.Text.StringBuilder, ByVal nSize As
Integer, _
ByVal
lpFileName As String) As Integer
End Function
<DllImport("KERNEL32.DLL",
CharSet:=CharSet.Auto)> _
Public Shared Function
GetPrivateProfileInt( _
ByVal lpAppName
As String, _
ByVal lpKeyName
As String, ByVal nDefault As Integer, _
ByVal
lpFileName As String) As Integer
End
Function |
リスト1
Win32 API
の GetPrivateProfileString 関数は String
型のデータを読み込む場合に使用し、GetPrivateProfileInt 関数は Integer
型のデータを読み込む場合に使用します。
続いて、これらの Win32 API
を呼び出します。実装コードは以下のとおりです。
|
Private Sub Button1_Click(ByVal
sender As System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Me.WritePrivateProfileString("Name",
"NameKey", Me.Name, "C:\Test.ini")
Me.WritePrivateProfileString("Size",
"WidthKey", Me.Width, "C:\Test.ini")
Me.WritePrivateProfileString("Size",
"HeightKey", Me.Height, "C:\Test.ini")
End Sub
Private Sub Button2_Click(ByVal
sender As System.Object, ByVal e As System.EventArgs) Handles
Button2.Click
Dim strSb As
System.Text.StringBuilder
strSb = New
System.Text.StringBuilder
Me.GetPrivateProfileString("Name",
"NameKey", "default", strSb, strSb.Capacity,
"C:\Test.ini")
Dim intWidth As
Integer
Dim intHeight
As Integer
intWidth =
Me.GetPrivateProfileInt("Size", "WidthKey", 0,
"C:\Test.ini")
intHeight =
Me.GetPrivateProfileInt("Size", "HeightKey", 0,
"C:\Test.ini")
MessageBox.Show("NameKey
= " & strSb.ToString() & Chr(13) & "WidthKey = "
& intWidth & Chr(13) & "HeightKey = " &
intHeight)
End
Sub |
リスト2
WritePrivateProfileString
関数によって C ドライブの Test.ini ファイルに各キーデータを書き込みます。C ドライブに Test.ini
ファイルが存在しない場合、Test.ini ファイルを新規作成します。図1 の「書き込み」ボタンをクリックすると、フォーム (図1) の
Name プロパティ、Width プロパティ、Height プロパティのデータ書き込みます (図2)。
.gif)
図1
.gif)
図2
GetPrivateProfileString 関数では String
型のデータを読み込み、GetPrivateProfileInt 関数では Integer 型のデータを読み込みます。リスト2
を実装し、「読み込み」ボタンをクリックすると、図4 のように C ドライブにある Test.ini ファイルから、NameKey
キー、WidthKey キー、HeightKey キーのデータを読み込みます。
.gif)
図3
構成ファイルVisual Basic .NET
では、アプリケーション構成ファイルという機能があります。構成ファイルは XML
形式で管理することができます。そこで続いては、構成ファイルへのデータの書き込みと読み込み方法について紹介します。今回は動的プロパティの利用法について紹介します。動的プロパティを使用することで、アプリケーションを再コンパイルせずにプロパティ値を変更することができます。フォームの
TopMost プロパティ値の変更を行います。
まず、Form1
のプロパティウィンドウの[(DynamicProperties)]-[(詳細)]で「'Form1'の動的プロパティ」ダイアログを表示します
(図4)。
.gif)
図4
ダイアログの「プロパティ」のリストより「TopMost」を選択し、「OK」ボタンをクリックします。すると、プロジェクトに「app.congif」ファイルが追加されます。
(図5)
.gif)
図5
アプリケーションをビルドすると、プロジェクト名.exe.congif
ファイルが作成されます。このファイルに Form1 の TopMost プロパティの内容が格納されます。これにより、アプリケーションで
TopMost プロパティの値を変更すると、自動的にプロジェクト名.exe.congif
に保存され、起動時にその値が使用されます。