Exemplo n.º 1
0
        private void btnSearchHex_Click(object sender, EventArgs e)
        {
            if (!File.Exists(txtFile.Text))
            {
                MessageBox.Show("檔案不存在!!");
                return;
            }
            if (string.IsNullOrEmpty(txtHexStr.Text))
            {
                MessageBox.Show("請輸入欲搜尋的 16 進位陣列字串!!");
                return;
            }
            this.UseWaitCursor = true;
            Application.DoEvents();
            string msg = string.Empty;

            byte[] sch = txtHexStr.Text.ToByteArray();
            int    idx = 0, start = 0;

            if (btnSearchHex.Tag != null)
            {
                start = Convert.ToInt32(btnSearchHex.Tag) + 1;
            }
            Stopwatch w = new Stopwatch();

            w.Start();
            if (!rbFindIndexes.Checked)
            {
                if (rbIndexOfBytes.Checked)
                {
                    byte[] buf = File.ReadAllBytes(txtFile.Text);
                    idx = buf.IndexOfBytes(sch, start);
                }
                else if (rbFindIndex.Checked)
                {
                    idx = ConvUtils.IndexOfBytesInFile(txtFile.Text, sch, start);
                }
                w.Stop();
                if (idx == -1)
                {
                    msg = string.Format("找不到欲搜尋的 16 進位陣列字串!!\n耗時:{0}ms / {1}ticks\n\n", w.ElapsedMilliseconds, w.ElapsedTicks);
                    btnSearchHex.Tag = null;
                }
                else
                {
                    msg = string.Format("欲搜尋的 16 進位陣列字串的索引值為 {0:X8}\n耗時:{1}ms / {2}ticks\n\n", idx, w.ElapsedMilliseconds, w.ElapsedTicks);
                    btnSearchHex.Tag = idx;
                }
            }
            else
            {
                int[] idxs = ConvUtils.IndexesOfBytesInFile(txtFile.Text, sch);
                w.Stop();
                if (idxs.Length == 0)
                {
                    msg = string.Format("找不到欲搜尋的 16 進位陣列字串!!\n耗時:{0}ms / {1}ticks\n\n", w.ElapsedMilliseconds, w.ElapsedTicks);
                    btnSearchHex.Tag = null;
                }
                else
                {
                    msg = string.Format("搜尋 16 進位陣列字串耗時:{0}ms / {1}ticks, 索引值為:\n", w.ElapsedMilliseconds, w.ElapsedTicks);
                    foreach (int ii in idxs)
                    {
                        msg += string.Format("{0:X8}\n", ii);
                    }
                    btnSearchHex.Tag = null;
                }
            }
            this.UseWaitCursor = false;
            Application.DoEvents();
            MessageBox.Show(msg);
        }