ini ファイルのリード・ライト
開発環境: Visual Studio 2003
1.目次 | ||||
2.目的 | ||||
| ||||
3.参考書 | ||||
(1) VisualStudio.NET のヘルプ(アンマネージ DLL 関数の処理) | ||||
4.アンマネージド関数の宣言部分 | ||||
| ||||
5.ini ファイルの読み込み | ||||
uint entryLength; 注意: | ||||
6.ini ファイルへの書き込み | ||||
string str = this.textBox1.Text; | ||||
7. Windows.Forms ベースのサンプルコード | ||||
以下のサンプルコードでは、C:\TEMP\Test.ini ファイル へのリード、ライトを行う Windows.Forms ベースのアプリケーションです。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsApplication11
{
/// <summary>
/// Form1 の概要の説明です。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必要なデザイナ変数です。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows フォーム デザイナ サポートに必要です。
//
InitializeComponent();
//
// TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
//
}
/// <summary>
/// 使用されているリソースに後処理を実行します。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// デザイナ サポートに必要なメソッドです。このメソッドの内容を
/// コード エディタで変更しないでください。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(184, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 40);
this.button1.TabIndex = 0;
this.button1.Text = "ini ファイルからの読み込み";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label1
//
this.label1.Location = new System.Drawing.Point(32, 40);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 24);
this.label1.TabIndex = 1;
this.label1.Text = "label1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(184, 88);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(80, 32);
this.button2.TabIndex = 2;
this.button2.Text = "ini ファイルへの書き込み";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 88);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(128, 19);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
this.ClientSize = new System.Drawing.Size(280, 166);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.button2,
this.label1,
this.button1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// アプリケーションのメイン エントリ ポイントです。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
/// <summary>
/// ini ファイルの読み込み用の関数(GetPrivateProfileString)の宣言部分
/// </summary>
[DllImport("kernel32.dll", EntryPoint="GetPrivateProfileString")]
private static extern uint GetPrivateProfileString( string lpApplicationName,
string lpEntryName, string lpDefault, System.Text.StringBuilder lpReturnedString,
uint nSize, string lpFileName );
/// <summary>
/// ini ファイルの書き込み用の関数(WritePrivateProfileString)の宣言部分
/// </summary>
[DllImport("kernel32.dll", EntryPoint="WritePrivateProfileString")]
private static extern uint WritePrivateProfileString( string lpApplicationName,
string lpEntryName, string lpEntryString, string lpFileName );
private void button1_Click(object sender, System.EventArgs e)
{
uint entryLength;
string strEntryStringValue;
// 読み込むためのバッファ
// string は、immutable (変更できない)ので、StringBuilder を使って、
// 次のようにバッファを渡してあげる必要があります。
System.Text.StringBuilder strEntryString = new System.Text.StringBuilder( 256 );
entryLength = GetPrivateProfileString( "SECTION", "ENTRY", "Nothing",
strEntryString, (uint)(strEntryString.Capacity), "C:\\TEMP\\TEST.ini" );
strEntryStringValue = strEntryString.ToString();
// 結果をラベルに書く。
this.label1.Text = strEntryStringValue;
}
private void button2_Click(object sender, System.EventArgs e)
{
// ini ファイルの SECTION に書き込む値を TextBox からとってくる。
string str = this.textBox1.Text;
uint entryLength;
entryLength = WritePrivateProfileString( "SECTION", "ENTRY", str, "C:\\TEMP\\TEST.ini" );
}
}
}
|
