▼.Net Compact Framework 2.0メモ▼
ファイルの読み書き


ファイルの読み書きを行うプログラムを作成する。



デザインの編集
FileForm
プロパティ
Text FileEx
MinimizeBox false
FormFactor Windows Mobile 6 Professional VGA


TextBox - tbText
プロパティ
Text (なし)
Location 20,40
Size 300,41

Button - btnRead
プロパティ
Text 読み込み
Location 20,100
イベント メソッド
Click btnRead_Click

Button - btnWrite
プロパティ
Text 書き込み
Location 180,100
イベント メソッド
Click btnWrite_Click


ソースコードの編集
FileForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace FileEx {
    public partial class FileForm : Form {
        //コンストラクタ
        public FileForm() {
            InitializeComponent();
        }

        //読み込みボタンのイベント処理
        private void btnRead_Click(object sender, EventArgs e) {
            StreamReader sr=null;
            try {
                sr=new StreamReader("My Documents\\test.txt");
                tbText.Text=sr.ReadLine();
                sr.Close();
            } catch (Exception exc) {
                if (sr!=null) sr.Close();
                MessageBox.Show(exc.ToString(),"エラー");
            }
        }

        //書き込みボタンのイベント処理
        private void btnWrite_Click(object sender, EventArgs e) {
            StreamWriter sw=null;
            try {
                sw=new StreamWriter("My Documents\\test.txt");
                sw.WriteLine(tbText.Text);
                sw.Close();
            } catch (Exception exc) {
                if (sw!=null) sw.Close();
                MessageBox.Show(exc.ToString(),"エラー");
            }
        }
    }
}



−戻る−