2009年1月14日 星期三

C# 讀檔、開檔、存檔

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

namespace 讀入TXT檔案至TEXTBOX
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)
{
//先清空之前的資料
listBox1.Items.Clear();
listBox2.Items.Clear();
listBox3.Items.Clear();

//檔案對話方塊,只選檔案而已,沒有讀入
openFileDialog1.ShowDialog();
textBox1.Text = openFileDialog1.FileName;
if (openFileDialog1.FileName.IndexOf("DCN") == -1)
{
MessageBox.Show("死小古,你選錯檔案了\n\n你應該選擇由 DCN 開頭的檔案", "你選錯了");
return;
}
int qq1 = 0;
int qq2 = 0;
//按下按鈕 讀入檔案 將 textbox1內容 讀至 listbox1
System.IO.StreamReader fn = new System.IO.StreamReader(textBox1.Text, Encoding.Default);
while (fn.EndOfStream == false)
{

string aa = fn.ReadLine();

listBox1.Items.Add(aa);
//判ok到 listbox2
if (aa.IndexOf("PANEL ID OK") >= 0)
{
listBox2.Items.Add(aa);
qq1++;

}
//判NG到 listbox3
if (aa.IndexOf("Panel ID Not Found") >= 0)
{
listBox3.Items.Add(aa);
qq2++;
}
}
fn.Close();
//將數量秀出
label4.Text = qq1.ToString();
label5.Text = qq2.ToString();
}
//將 Listbox3 NG內容 存至固定位置.


private void button2_Click(object sender, EventArgs e)
{

//選擇目錄
saveFileDialog1.ShowDialog();
textBox3.Text = saveFileDialog1.FileName;

if (textBox3.Text == "")
{
MessageBox.Show("請選擇目錄,小古!");
return;
}

//Save
System.IO.StreamWriter fn = new System.IO.StreamWriter(textBox3.Text);
for (int fu = 0; fu < path =" new" text =" path.SelectedPath;" text ="=" qq1 =" 0;" qq2 =" 0;" qq3 =" 0;" qq4 =" 0;" text =" qq1.ToString();" text =" qq2.ToString();">= 0)
{
listBox1.Items.Add(xx);
//將listbox1 內檔案順序開到 listbox2 (OK),listbox3(NG)
System.IO.StreamReader fcc = new System.IO.StreamReader(xx, Encoding.Default);
while (fcc.EndOfStream == false)
{

string bb = fcc.ReadLine();

//判ok到 listbox2
if (bb.IndexOf("PANEL ID OK") >= 0)
{
listBox2.Items.Add(bb);
qq3++;

}
//判NG到 listbox3
if (bb.IndexOf("Panel ID Not Found") >= 0)
{
listBox3.Items.Add(bb);
qq4++;
}
}
fcc.Close();
//將數量秀出
label4.Text = qq3.ToString();
label5.Text = qq4.ToString();
}
}
}
}
}

沒有留言:

張貼留言