ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
ini ファイルアクセス ( No.2 )
日時: 2008/10/19 14:08
名前: lightbox



関数として
Imports System.Text
Imports System.Runtime.InteropServices

Module Module2

	' ******************************************************
	' ini ファイル書き込み
	' ******************************************************
	<DllImport("Kernel32.dll", CharSet:=CharSet.Auto)> _
	Public Function WritePrivateProfileString( _
	   ByVal lpAppName As String, _
	   ByVal lpKeyName As String, _
	   ByVal lpString As String, _
	   ByVal lpFileName As String) As Integer
	End Function

	' ******************************************************
	' ini ファイル読み込み
	' ******************************************************
	<DllImport("Kernel32.dll", CharSet:=CharSet.Auto)> _
	Public Function GetPrivateProfileString( _
	   ByVal lpAppName As String, _
	   ByVal lpKeyName As String, _
	   ByVal lpDefault As String, _
	   ByVal lpReturnedString As StringBuilder, _
	   ByVal nSize As Integer, _
	   ByVal lpFileName As String) As Integer
	End Function

End Module
Imports System.Text
Imports System.Windows.Forms

Module Module1

	Sub Main()

		Dim ini As String = Application.StartupPath() & "\connect.ini"

		Module2.WritePrivateProfileString("Database", "Oracle", "localhost/ORCL", ini)

		Dim str As StringBuilder = New StringBuilder(512)

		GetPrivateProfileString("Database", "Oracle", "", str, 512, ini)

		Console.WriteLine(str.ToString())

		Console.ReadLine()

	End Sub

End Module

クラスとして
Imports System.Text
Imports System.Runtime.InteropServices

Public Class INI

	Public ini As String

	' ******************************************************
	' ini ファイル読み込み
	' ******************************************************
	<DllImport("Kernel32.dll", CharSet:=CharSet.Auto)> _
	Private Shared Function GetPrivateProfileString( _
	   ByVal lpAppName As String, _
	   ByVal lpKeyName As String, _
	   ByVal lpDefault As String, _
	   ByVal lpReturnedString As StringBuilder, _
	   ByVal nSize As Integer, _
	   ByVal lpFileName As String) As Integer
	End Function

	' ******************************************************
	' ini ファイル書き込み
	' ******************************************************
	<DllImport("Kernel32.dll", CharSet:=CharSet.Auto)> _
	Private Shared Function WritePrivateProfileString( _
	   ByVal lpAppName As String, _
	   ByVal lpKeyName As String, _
	   ByVal lpString As String, _
	   ByVal lpFileName As String) As Integer
	End Function

	' ********************************************************
	' (コンストラクタの定義)( Sub で定義する )
	' ********************************************************
	Public Sub New(ByVal path As String)

		ini = path

	End Sub

	' ******************************************************
	' 取得
	' ******************************************************
	Public Function GetValue(ByVal Section As String, ByVal Entry As String) As String

		Dim str As StringBuilder = New StringBuilder(512)

		GetPrivateProfileString(Section, Entry, "", str, 512, ini)

		GetValue = str.ToString()

	End Function

	' ******************************************************
	' 書き込み
	' ******************************************************
	Public Sub SetValue(ByVal Section As String, ByVal Entry As String, ByVal Value As String)

		WritePrivateProfileString(Section, Entry, Value, ini)

	End Sub

End Class
Imports System.Windows.Forms

Module Module1

	Sub Main()

		Dim ini As INI = New INI(Application.StartupPath() & "\connect.ini")

		ini.SetValue("Database", "Oracle", "localhost/ORCL")

		Console.WriteLine(ini.GetValue("Database", "Oracle"))

		Console.ReadLine()

	End Sub

End Module