ファイルの中の文字を探すツールの作り方2(ソースコード)

このソースは「ファイルの中の文字を探すツールの作り方」の内容です

 

ソースコードはこちらになります

 

 

       private void listBox1_DragDrop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                foreach (string fileName
                    in (string[])e.Data.GetData(DataFormats.FileDrop))
                {
                    listBox1.Items.Add(fileName);
                }
            }
        }

        private void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                for (int i = 0; i <= listBox1.SelectedItems.Count; i++)
                {
                    listBox1.Items.Remove(listBox1.SelectedItem);
                }
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
        }
        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            string path;
            string src_utf;
            string src_sift;
            int datacnt = 0;
            System.IO.StreamReader reader;

            string key_old = textBox2.Text;

            bool ChangeFlg;

            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                path = listBox1.Items[i].ToString();

                string[] files = { path };

                if (System.IO.File.Exists(path))
                {
                    // ファイルだ!
                }
                else
                {
                       //ディレクトリだ!
                    files = System.IO.Directory.GetFiles(path, "*", System.IO.SearchOption.AllDirectories);
                }



                foreach (string searchfile in files)
                {

                    label1.Text = searchfile;
                    System.Windows.Forms.Application.DoEvents();

                    reader = new System.IO.StreamReader(searchfile, System.Text.Encoding.GetEncoding("utf-8"));
                    src_utf = reader.ReadToEnd();
                    reader.Close();

                    reader = new System.IO.StreamReader(searchfile, System.Text.Encoding.GetEncoding("shift_jis"));
                    src_sift = reader.ReadToEnd();
                    reader.Close();

                   if (src_utf.IndexOf(key_old) != -1)
                   {
                       textBox1.Text = textBox1.Text + searchfile + "\r\n";
                       datacnt++;
                   }else if(src_sift.IndexOf(key_old) != -1){
                       textBox1.Text = textBox1.Text + searchfile + "\r\n";
                       datacnt++;
                   }
               }

            }

            label1.Text = "FIX=" + datacnt.ToString() ;


            MessageBox.Show("OK");
        }

コメントを残す