close

小菜菜最近碰上一個問題,發現Flash裡面有存在不該有的資料,跟燒錄組詢問,得到的答覆是Flash是全新的,沒有使用過。

基於同事情誼,碰上這種事,只好發揮柯南精神,寫一個笨笨的BIN 檔搜尋器,尋找浩瀚的BIN資料庫,

確認Flash內的"髒污"是我們自己的東西,不是供應商的來料問題。

參考Tony的code ==> http://blog.tonycube.com/2011/04/backgroundworker.html

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 BinaryReader
{
    public partial class Form1 : Form
    {
        byte[] newData = new byte[7];
        string show_now_bin;
        bool show_report_flag = false;
        string show_report;
        int bar_cnt = 0;
        
        private BackgroundWorker bw;
        public Form1()
        {
            InitializeComponent();
            initProgressBar();
            initBackgroundWorker();
        }

        private void initProgressBar()
        {
            progressBar1.Step = 1;
        }

        private void initBackgroundWorker()
        {
            bw = new BackgroundWorker();
            bw.WorkerReportsProgress = true;      // 設定是否可以接收進度報告。(才能引發ProgressChanged,改變UI)
            bw.WorkerSupportsCancellation = true; // 設定是否支援非同步取消。
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);  // 背景執行的code,不能有UI互動元件
            bw.ProgressChanged += new ProgressChangedEventHandler(bw_ProgressChanged); // 執行背景report的進度
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
        }
        //背景執行
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            long bin_lenth;
            long move_point;
            long k = 0;

            // 在DoWork事件的處理函式裡不能有任何和UI元件的互動。
            if ((bw.CancellationPending == true))
            {
                e.Cancel = true;
            }
            else
            {
                foreach (string checkfile in Directory.EnumerateFiles(textBox1.Text, "*.bin", SearchOption.AllDirectories))
                {
                    show_now_bin = checkfile;

                    FileStream myFile = new FileStream(checkfile, FileMode.Open);
                    byte[] byDataValue = new byte[7];
                    bar_cnt = 0;

                    // 取得檔案長度
                    bin_lenth = myFile.Length;

                    for (move_point = 0; move_point < (bin_lenth - 5); move_point++)
                    {
                        // 移動文件指針
                        myFile.Seek(move_point, SeekOrigin.Begin);
                        // 將接下來的字節讀到Array中
                        myFile.Read(byDataValue, 0, 6);
                        // 比較內容
                        if (BitConverter.ToString(newData) == BitConverter.ToString(byDataValue))
                        {
                            show_report = checkfile + " Is same.....";
                            show_report_flag = true;
                            if (bar_cnt >= 100)
                                bar_cnt = 100;
                            bw.ReportProgress(bar_cnt);
                            break;
                            // ‧break:直接跳出for迴圈後,會繼續執行迴圈外的程式
                            // ‧return :直接跳出結束那個函式,不會繼續執行迴圈外的程式
                            // ‧continue:跳過當前循環體中的當次迴圈(i),進入下一次迴圈(i+1),並回上一層繼續執行迴圈外的程式
                        }
                        // 進度表
                        k = bin_lenth / 100;
                        if (move_point == (bar_cnt * k))
                        {
                            // ReportProgress()方法:在DoWork事件處理函式中,送出進度報告,會被ProgressChanged事件接收。
                            if (bar_cnt >= 100)
                                bar_cnt = 100;
                            bw.ReportProgress(bar_cnt);  // only 0-100
                            bar_cnt++;
                        }
                    }
                    //釋放資源
                    myFile.Close();
                }
            }
        }
        //處理進度,可以和UI元件互動。
        private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (bar_cnt == 1)
            {
                textBox2.Text = show_now_bin;
                textBox2.Refresh();
                listBox1.Items.Add(show_now_bin);
                listBox1.Refresh();
            }

            if (show_report_flag == true)
            {
                show_report_flag = false;
                listBox2.Items.Add(show_report);
                listBox2.Refresh();
            }

            progressBar1.Value = e.ProgressPercentage;
            listBox1.Text = e.ProgressPercentage.ToString();
            listBox1.Refresh();
        }

        //執行完成,可以和UI元件互動。
        private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if ((e.Cancelled == true))    // CancellationPending ?
            {
                listBox1.Items.Add("取消!");  // ???
                listBox1.Refresh();
            }
            else if (!(e.Error == null))
            {
                listBox1.Text = ("Error: " + e.Error.Message);
            }
            else
            {
                listBox1.Items.Add("完成!");
                listBox1.Refresh();
            }
        }
        // 選擇要搜尋的目錄
        private void btnStart_Click(object sender, EventArgs e)
        {
            int i;
            int k1;
            int k2;
            int merge_new;

            if (textBox1.Text == "")
            {
                MessageBox.Show("請設定起始資料夾");
                return;
            }

            if (textBox3.Text.Length != 12)
            {
                MessageBox.Show("輸入字數限定12碼");
                return;
            }

            #region 將輸入將比對12字資料轉成6Byte HEX格式
            byte[] myData = new byte[13];

            string comp_input = textBox3.Text;
            for (i = 0; i < 6; i++)
            {
                string comp1 = comp_input[i * 2].ToString();
                string comp2 = comp_input[i * 2 + 1].ToString();
                k1 = (Convert.ToByte(comp1, 16)) * 16;
                k2 = Convert.ToByte(comp2, 16);
                merge_new = k1 + k2;
                newData[i] = Convert.ToByte(merge_new);
                // listBox1.Items.Add(newData[i]);
                // listBox1.Refresh();
            }
            #endregion

            if (bw.IsBusy != true)
            {
                listBox1.Items.Add("開始");
                listBox1.Refresh();
                progressBar1.Value = 0;
                bw.RunWorkerAsync();  // 啟動背景程序
            }
        }

        private void btnFinish_Click(object sender, EventArgs e)
        {
            // 1. CancelAsync 方法:呼叫這個方法會送出停止背景作業的要求,並將 CancellationPending 屬性設為 true。
            if (bw.WorkerSupportsCancellation == true)
            {
                bw.CancelAsync();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 設定一目錄給 textBox1.
            // 參考 http://readily-notes.blogspot.tw/2010/10/folderbrowserdialog.html
            FolderBrowserDialog FolderBrowserDialog1 = new FolderBrowserDialog();
            FolderBrowserDialog1.Description = "Please select one folder";
            FolderBrowserDialog1.ShowNewFolderButton = false;

            if (FolderBrowserDialog1.ShowDialog() == DialogResult.Cancel)
                return;
            textBox1.Text = FolderBrowserDialog1.SelectedPath;
        }
    }
}

 

arrow
arrow
    文章標籤
    程式
    全站熱搜

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