Пример #1
0
 private void FileSearchAddResult(string result)
 {
     try
     {
         if (InvokeRequired)
         {
             Invoke(new Action(() => { FileSearchAddResult(result); }));
         }
         else
         {
             FileSearchResultsTextBox.AppendText(result + "\r\n");
         }
     }
     catch { }
 }
Пример #2
0
        private void FileSearchButton_Click(object sender, EventArgs e)
        {
            string searchtxt    = FileSearchTextBox.Text;
            string searchfolder = FileSearchFolderTextBox.Text;

            AbortOperation = false;

            if (InProgress)
            {
                return;
            }
            if (searchfolder.Length == 0)
            {
                MessageBox.Show("Please select a folder...");
                return;
            }
            if (!Directory.Exists(searchfolder))
            {
                MessageBox.Show("Please select a valid folder!");
                return;
            }

            FileSearchResultsTextBox.Clear();

            byte[] searchbytes1;
            byte[] searchbytes2;
            int    bytelen;

            if (FileSearchHexRadio.Checked)
            {
                try
                {
                    bytelen      = searchtxt.Length / 2;
                    searchbytes1 = new byte[bytelen];
                    searchbytes2 = new byte[bytelen];
                    for (int i = 0; i < bytelen; i++)
                    {
                        searchbytes1[i] = Convert.ToByte(searchtxt.Substring(i * 2, 2), 16);
                        searchbytes2[bytelen - i - 1] = searchbytes1[i];
                    }
                }
                catch
                {
                    MessageBox.Show("Please enter a valid hex string.");
                    return;
                }
            }
            else
            {
                bytelen      = searchtxt.Length;
                searchbytes1 = new byte[bytelen];
                searchbytes2 = new byte[bytelen];
                for (int i = 0; i < bytelen; i++)
                {
                    searchbytes1[i] = (byte)searchtxt[i];
                    searchbytes2[bytelen - i - 1] = searchbytes1[i];
                }
            }

            FileSearchPanel.Enabled = false;

            InProgress = true;

            Task.Run(() =>
            {
                FileSearchAddResult("Searching " + searchfolder + "...");

                string[] filenames = Directory.GetFiles(searchfolder);

                int matchcount = 0;

                foreach (string filename in filenames)
                {
                    FileInfo finf    = new FileInfo(filename);
                    byte[] filebytes = File.ReadAllBytes(filename);

                    int hitlen1 = 0;
                    int hitlen2 = 0;

                    for (int i = 0; i < filebytes.Length; i++)
                    {
                        byte b  = filebytes[i];
                        byte b1 = searchbytes1[hitlen1]; //current test byte 1
                        byte b2 = searchbytes2[hitlen2];

                        if (b == b1)
                        {
                            hitlen1++;
                        }
                        else
                        {
                            hitlen1 = 0;
                        }
                        if (b == b2)
                        {
                            hitlen2++;
                        }
                        else
                        {
                            hitlen2 = 0;
                        }

                        if (hitlen1 == bytelen)
                        {
                            FileSearchAddResult(finf.Name + ":" + (i - bytelen));
                            matchcount++;
                            hitlen1 = 0;
                        }
                        if (hitlen2 == bytelen)
                        {
                            FileSearchAddResult(finf.Name + ":" + (i - bytelen));
                            matchcount++;
                            hitlen2 = 0;
                        }

                        if (AbortOperation)
                        {
                            FileSearchAddResult("Search aborted.");
                            FileSearchComplete();
                            InProgress = false;
                            return;
                        }
                    }
                }

                FileSearchAddResult(string.Format("Search complete. {0} results found.", matchcount));
                FileSearchComplete();
                InProgress = false;
            });
        }