using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ReadLine
{
     public partial class Form1 : Form
     {
         public Form1()
         {
             InitializeComponent();
         }

        private void button1_Click(object sender, EventArgs e)
         {
             using (OpenFileDialog OFD = new OpenFileDialog())
             {
                 try
                 {
                     OFD.Filter = "文字檔(*.txt)|*.txt";
                     OFD.ShowDialog();
                     textBox1.Text = OFD.FileName;
                     StreamReader SReader = new StreamReader(textBox1.Text, Encoding.Default);
                     textBox2.Text = SReader.ReadLine();
                     SReader.Close();
                 }
                 catch
                 {
                 }
             }
         }

        private void button2_Click(object sender, EventArgs e)
         {
             using (SaveFileDialog SFD = new SaveFileDialog())
             {
                 try
                 {
                     SFD.Filter = "文字檔(*.txt)|*.txt";
                     SFD.ShowDialog();
                     textBox3.Text = SFD.FileName;
                     StreamWriter SWriter = new StreamWriter(textBox3.Text);
                     SWriter.WriteLine(textBox2.Text);
                 }
                 catch
                 {
                 }
             }
         }

        private void Form1_Load(object sender, EventArgs e)
         {

        }
     }
}

image

還蠻聰明,遇到已經存在的檔名會自動跳出提示”是否取代”!

image

sample code

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()