Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Diagnostics;
- namespace FileSearcher
- {
- public partial class MainWindow : Form
- {
- private Boolean m_closing = false;
- private delegate void FoundInfoSyncHandler(FoundInfoEventArgs e);
- private FoundInfoSyncHandler FoundInfo;
- private delegate void ThreadEndedSyncHandler(ThreadEndedEventArgs e);
- private ThreadEndedSyncHandler ThreadEnded;
- public MainWindow()
- {
- InitializeComponent();
- }
- private void MainWindow_Load(object sender, EventArgs e)
- {
- UserConfig.Load();
- searchDirTextBox.Text = UserConfig.Data.SearchDir;
- fileNameTextBox.Text = UserConfig.Data.FileName;
- this.FoundInfo += new FoundInfoSyncHandler(this_FoundInfo);
- this.ThreadEnded += new ThreadEndedSyncHandler(this_ThreadEnded);
- Searcher.FoundInfo += new Searcher.FoundInfoEventHandler(Searcher_FoundInfo);
- Searcher.ThreadEnded += new Searcher.ThreadEndedEventHandler(Searcher_ThreadEnded);
- }
- private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
- {
- m_closing = true;
- Searcher.Stop();
- if (this.WindowState != FormWindowState.Minimized)
- {
- UserConfig.Data.WindowState = (Int32)this.WindowState;
- }
- UserConfig.Data.SearchDir = searchDirTextBox.Text;
- UserConfig.Save();
- }
- private void selectSearchDirButton_Click(object sender, EventArgs e)
- {
- FolderBrowserDialog dlg = new FolderBrowserDialog();
- dlg.SelectedPath = searchDirTextBox.Text;
- dlg.ShowNewFolderButton = false;
- if (dlg.ShowDialog(this) == DialogResult.OK)
- {
- searchDirTextBox.Text = dlg.SelectedPath;
- }
- }
- private void startButton_Click(object sender, EventArgs e)
- {
- this.timer1.Enabled = true;
- this.progressBar1.Value = 100;
- this.timer1.Interval = 1;
- resultsList.Items.Clear();
- String fileNamesString = fileNameTextBox.Text;
- String[] fileNames = fileNamesString.Split(new Char[]{';'});
- List<String> validFileNames = new List<String>();
- foreach (String fileName in fileNames)
- {
- String trimmedFileName = fileName.Trim();
- if (trimmedFileName != "")
- {
- validFileNames.Add(trimmedFileName);
- }
- }
- Encoding encoding = asciiRadioButton.Checked ? Encoding.ASCII : Encoding.Unicode;
- SearcherParams pars = new SearcherParams( searchDirTextBox.Text.Trim(),
- includeSubDirsCheckBox.Checked,
- validFileNames,
- newerThanCheckBox.Checked,
- newerThanDateTimePicker.Value,
- olderThanCheckBox.Checked,
- olderThanDateTimePicker.Value,
- containingCheckBox.Checked,
- containingTextBox.Text.Trim(),
- encoding);
- if (Searcher.Start(pars))
- {
- }
- else
- {
- MessageBox.Show("Proces je již spuštěn.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- }
- }
- private void newerThanCheckBox_CheckedChanged(object sender, EventArgs e)
- {
- }
- private void olderThanCheckBox_CheckedChanged(object sender, EventArgs e)
- {
- }
- private void containingCheckBox_CheckedChanged(object sender, EventArgs e)
- {
- }
- private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
- {
- if (resultsList.SelectedItems.Count == 0)
- {
- e.Cancel = true;
- }
- }
- private void openContainingFolderContextMenuItem_Click(object sender, EventArgs e)
- {
- if (resultsList.SelectedItems.Count > 0)
- {
- String path = resultsList.SelectedItems[0].Text;
- try
- {
- ProcessStartInfo startInfo = new ProcessStartInfo();
- startInfo.FileName = "explorer.exe";
- startInfo.Arguments = Path.GetDirectoryName(path);
- Process process = new Process();
- process.StartInfo = startInfo;
- process.Start();
- }
- catch (Exception)
- {
- }
- }
- }
- private void resultsList_DoubleClick(object sender, EventArgs e)
- {
- if (resultsList.SelectedItems.Count > 0)
- {
- String path = resultsList.SelectedItems[0].Text;
- if (File.Exists(path))
- {
- try
- {
- ProcessStartInfo startInfo = new ProcessStartInfo();
- startInfo.FileName = path;
- startInfo.Arguments = "";
- Process process = new Process();
- process.StartInfo = startInfo;
- process.Start();
- }
- catch (Exception)
- {
- }
- }
- }
- }
- private void resultsList_Resize(object sender, EventArgs e)
- {
- resultsList.Columns[0].Width = resultsList.Width - 230;
- }
- private void Searcher_FoundInfo(FoundInfoEventArgs e)
- {
- if (!m_closing)
- {
- this.Invoke(FoundInfo, new object[] { e });
- }
- }
- private void this_FoundInfo(FoundInfoEventArgs e)
- {
- CreateResultsListItem(e.Info);
- }
- private void Searcher_ThreadEnded(ThreadEndedEventArgs e)
- {
- if (!m_closing)
- {
- this.Invoke(ThreadEnded, new object[] { e });
- }
- }
- private void this_ThreadEnded(ThreadEndedEventArgs e)
- {
- EnableButtons();
- if (!e.Success)
- {
- MessageBox.Show(e.ErrorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- }
- }
- private void EnableButtons()
- {
- }
- private void DisableButtons()
- {
- }
- public static String GetPointString(Int64 value)
- {
- String pointString = value.ToString();
- Int32 i = 3;
- while (pointString.Length > i)
- {
- pointString = pointString.Substring(0, pointString.Length - i) + "." + pointString.Substring(pointString.Length - i, i);
- i += 4;
- }
- return pointString;
- }
- private void CreateResultsListItem(FileSystemInfo info)
- {
- ListViewItem lvi = new ListViewItem();
- lvi.Text = info.FullName;
- ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();
- lvi.SubItems.Add(lvsi);
- lvsi = new ListViewItem.ListViewSubItem();
- lvi.SubItems.Add(lvsi);
- lvi.ToolTipText = info.FullName;
- resultsList.Items.Add(lvi);
- }
- private void searchDirTextBox_TextChanged(object sender, EventArgs e)
- {
- }
- private void resultsList_SelectedIndexChanged_1(object sender, EventArgs e)
- {
- }
- private void oProgramuToolStripMenuItem_Click(object sender, EventArgs e)
- {
- System.Windows.Forms.MessageBox.Show("Autor: (C) Copyright 2012 Edison", "O Programu", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- private void zavřítToolStripMenuItem_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void label2_Click(object sender, EventArgs e)
- {
- }
- private void button1_Click(object sender, EventArgs e)
- {
- SaveFileDialog dlg = new SaveFileDialog();
- dlg.Filter = "Playlistové soubory|*.lst|Textové soubory|*.txt";
- dlg.FilterIndex = 1;
- dlg.FileName = UserConfig.Data.ResultsFilePath;
- if (dlg.ShowDialog(this) == DialogResult.OK)
- {
- textBox1.Text = dlg.FileName;
- UserConfig.Data.ResultsFilePath = dlg.FileName;
- try
- {
- FileStream fs = new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write);
- StreamWriter sw = new StreamWriter(fs);
- foreach (ListViewItem lvi in resultsList.Items)
- {
- bool bFirstLvsi = true;
- foreach (ListViewItem.ListViewSubItem lvsi in lvi.SubItems)
- {
- if (bFirstLvsi)
- {
- bFirstLvsi = false;
- sw.Write(lvsi.Text);
- }
- else
- {
- sw.Write(lvsi.Text);
- }
- }
- }
- sw.Close();
- fs.Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- }
- }
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- if (this.progressBar1.Value < 100)
- {
- this.progressBar1.Value++;
- if (this.progressBar1.Value == 100)
- {
- this.timer1.Enabled = false;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement