Advertisement
Guest User

Untitled

a guest
Nov 29th, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.99 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Text;
  6. using System.Windows.Forms;
  7. using System.IO;
  8. using System.Diagnostics;
  9.  
  10.  
  11. namespace FileSearcher
  12. {
  13.     public partial class MainWindow : Form
  14.     {
  15.        
  16.         private Boolean m_closing = false;
  17.  
  18.         private delegate void FoundInfoSyncHandler(FoundInfoEventArgs e);
  19.         private FoundInfoSyncHandler FoundInfo;
  20.  
  21.         private delegate void ThreadEndedSyncHandler(ThreadEndedEventArgs e);
  22.         private ThreadEndedSyncHandler ThreadEnded;
  23.  
  24.         public MainWindow()
  25.         {
  26.             InitializeComponent();
  27.         }
  28.  
  29.         private void MainWindow_Load(object sender, EventArgs e)
  30.         {
  31.             UserConfig.Load();
  32.  
  33.             searchDirTextBox.Text = UserConfig.Data.SearchDir;
  34.             fileNameTextBox.Text = UserConfig.Data.FileName;
  35.  
  36.             this.FoundInfo += new FoundInfoSyncHandler(this_FoundInfo);
  37.             this.ThreadEnded += new ThreadEndedSyncHandler(this_ThreadEnded);
  38.  
  39.             Searcher.FoundInfo += new Searcher.FoundInfoEventHandler(Searcher_FoundInfo);
  40.             Searcher.ThreadEnded += new Searcher.ThreadEndedEventHandler(Searcher_ThreadEnded);
  41.         }
  42.  
  43.         private void MainWindow_FormClosing(object sender, FormClosingEventArgs e)
  44.         {
  45.             m_closing = true;
  46.  
  47.             Searcher.Stop();
  48.            
  49.             if (this.WindowState != FormWindowState.Minimized)
  50.             {
  51.                 UserConfig.Data.WindowState = (Int32)this.WindowState;
  52.             }
  53.  
  54.             UserConfig.Data.SearchDir = searchDirTextBox.Text;
  55.            
  56.             UserConfig.Save();
  57.         }
  58.  
  59.         private void selectSearchDirButton_Click(object sender, EventArgs e)
  60.         {
  61.             FolderBrowserDialog dlg = new FolderBrowserDialog();
  62.             dlg.SelectedPath = searchDirTextBox.Text;
  63.             dlg.ShowNewFolderButton = false;
  64.             if (dlg.ShowDialog(this) == DialogResult.OK)
  65.             {
  66.                 searchDirTextBox.Text = dlg.SelectedPath;
  67.             }
  68.         }
  69.  
  70.         private void startButton_Click(object sender, EventArgs e)
  71.         {
  72.  
  73.             this.timer1.Enabled = true;
  74.             this.progressBar1.Value = 100;
  75.             this.timer1.Interval = 1;
  76.            
  77.             resultsList.Items.Clear();
  78.  
  79.             String fileNamesString = fileNameTextBox.Text;
  80.             String[] fileNames = fileNamesString.Split(new Char[]{';'});
  81.             List<String> validFileNames = new List<String>();
  82.             foreach (String fileName in fileNames)
  83.             {
  84.                 String trimmedFileName = fileName.Trim();
  85.                 if (trimmedFileName != "")
  86.                 {
  87.                     validFileNames.Add(trimmedFileName);
  88.                 }
  89.             }
  90.  
  91.             Encoding encoding = asciiRadioButton.Checked ? Encoding.ASCII : Encoding.Unicode;
  92.  
  93.             SearcherParams pars = new SearcherParams(   searchDirTextBox.Text.Trim(),
  94.                                                         includeSubDirsCheckBox.Checked,
  95.                                                         validFileNames,
  96.                                                         newerThanCheckBox.Checked,
  97.                                                         newerThanDateTimePicker.Value,
  98.                                                         olderThanCheckBox.Checked,
  99.                                                         olderThanDateTimePicker.Value,
  100.                                                         containingCheckBox.Checked,
  101.                                                         containingTextBox.Text.Trim(),
  102.                                                         encoding);
  103.            
  104.             if (Searcher.Start(pars))
  105.             {
  106.                
  107.             }
  108.             else
  109.             {
  110.                 MessageBox.Show("Proces je již spuštěn.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  111.             }
  112.         }
  113.  
  114.         private void newerThanCheckBox_CheckedChanged(object sender, EventArgs e)
  115.         {
  116.            
  117.         }
  118.  
  119.         private void olderThanCheckBox_CheckedChanged(object sender, EventArgs e)
  120.         {
  121.            
  122.         }
  123.  
  124.         private void containingCheckBox_CheckedChanged(object sender, EventArgs e)
  125.         {
  126.  
  127.         }
  128.  
  129.         private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
  130.         {
  131.             if (resultsList.SelectedItems.Count == 0)
  132.             {
  133.                 e.Cancel = true;
  134.             }
  135.         }
  136.  
  137.         private void openContainingFolderContextMenuItem_Click(object sender, EventArgs e)
  138.         {
  139.             if (resultsList.SelectedItems.Count > 0)
  140.             {
  141.                 String path = resultsList.SelectedItems[0].Text;
  142.  
  143.                 try
  144.                 {
  145.                     ProcessStartInfo startInfo = new ProcessStartInfo();
  146.                     startInfo.FileName = "explorer.exe";
  147.                     startInfo.Arguments = Path.GetDirectoryName(path);
  148.                     Process process = new Process();
  149.                     process.StartInfo = startInfo;
  150.                     process.Start();
  151.                 }
  152.                 catch (Exception)
  153.                 {
  154.                 }
  155.             }
  156.         }
  157.  
  158.         private void resultsList_DoubleClick(object sender, EventArgs e)
  159.         {
  160.             if (resultsList.SelectedItems.Count > 0)
  161.             {
  162.                 String path = resultsList.SelectedItems[0].Text;
  163.  
  164.                 if (File.Exists(path))
  165.                 {
  166.                     try
  167.                     {
  168.                         ProcessStartInfo startInfo = new ProcessStartInfo();
  169.                         startInfo.FileName = path;
  170.                         startInfo.Arguments = "";
  171.                         Process process = new Process();
  172.                         process.StartInfo = startInfo;
  173.                         process.Start();
  174.                     }
  175.                     catch (Exception)
  176.                     {
  177.                     }
  178.                 }
  179.             }
  180.         }
  181.  
  182.         private void resultsList_Resize(object sender, EventArgs e)
  183.         {
  184.             resultsList.Columns[0].Width = resultsList.Width - 230;
  185.         }
  186.  
  187.         private void Searcher_FoundInfo(FoundInfoEventArgs e)
  188.         {
  189.             if (!m_closing)
  190.             {
  191.                 this.Invoke(FoundInfo, new object[] { e });
  192.             }
  193.         }
  194.  
  195.         private void this_FoundInfo(FoundInfoEventArgs e)
  196.         {
  197.             CreateResultsListItem(e.Info);
  198.         }
  199.  
  200.         private void Searcher_ThreadEnded(ThreadEndedEventArgs e)
  201.         {
  202.             if (!m_closing)
  203.             {
  204.                 this.Invoke(ThreadEnded, new object[] { e });
  205.             }
  206.         }
  207.  
  208.         private void this_ThreadEnded(ThreadEndedEventArgs e)
  209.         {
  210.             EnableButtons();
  211.  
  212.             if (!e.Success)
  213.             {
  214.                 MessageBox.Show(e.ErrorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  215.             }
  216.         }
  217.  
  218.         private void EnableButtons()
  219.         {
  220.              
  221.         }
  222.  
  223.         private void DisableButtons()
  224.         {
  225.            
  226.         }
  227.  
  228.         public static String GetPointString(Int64 value)
  229.         {
  230.             String pointString = value.ToString();
  231.  
  232.             Int32 i = 3;
  233.             while (pointString.Length > i)
  234.             {
  235.                 pointString = pointString.Substring(0, pointString.Length - i) + "." + pointString.Substring(pointString.Length - i, i);
  236.                 i += 4;
  237.             }
  238.  
  239.             return pointString;
  240.         }
  241.  
  242.         private void CreateResultsListItem(FileSystemInfo info)
  243.         {
  244.             ListViewItem lvi = new ListViewItem();
  245.             lvi.Text = info.FullName;
  246.  
  247.             ListViewItem.ListViewSubItem lvsi = new ListViewItem.ListViewSubItem();
  248.            
  249.             lvi.SubItems.Add(lvsi);
  250.  
  251.             lvsi = new ListViewItem.ListViewSubItem();
  252.             lvi.SubItems.Add(lvsi);
  253.  
  254.             lvi.ToolTipText = info.FullName;
  255.  
  256.             resultsList.Items.Add(lvi);
  257.         }
  258.  
  259.         private void searchDirTextBox_TextChanged(object sender, EventArgs e)
  260.         {
  261.  
  262.         }
  263.  
  264.         private void resultsList_SelectedIndexChanged_1(object sender, EventArgs e)
  265.         {
  266.  
  267.         }
  268.  
  269.         private void oProgramuToolStripMenuItem_Click(object sender, EventArgs e)
  270.         {
  271.             System.Windows.Forms.MessageBox.Show("Autor: (C) Copyright 2012 Edison", "O Programu", MessageBoxButtons.OK, MessageBoxIcon.Information);
  272.         }
  273.  
  274.         private void zavřítToolStripMenuItem_Click(object sender, EventArgs e)
  275.         {
  276.             Application.Exit();
  277.         }
  278.  
  279.         private void label2_Click(object sender, EventArgs e)
  280.         {
  281.  
  282.         }
  283.  
  284.         private void button1_Click(object sender, EventArgs e)
  285.         {
  286.             SaveFileDialog dlg = new SaveFileDialog();
  287.             dlg.Filter = "Playlistové soubory|*.lst|Textové soubory|*.txt";
  288.             dlg.FilterIndex = 1;
  289.             dlg.FileName = UserConfig.Data.ResultsFilePath;
  290.             if (dlg.ShowDialog(this) == DialogResult.OK)
  291.             {
  292.                 textBox1.Text = dlg.FileName;
  293.                 UserConfig.Data.ResultsFilePath = dlg.FileName;
  294.  
  295.                 try
  296.                 {
  297.                     FileStream fs = new FileStream(dlg.FileName, FileMode.Create, FileAccess.Write);
  298.                     StreamWriter sw = new StreamWriter(fs);
  299.  
  300.                     foreach (ListViewItem lvi in resultsList.Items)
  301.                     {
  302.                         bool bFirstLvsi = true;
  303.                         foreach (ListViewItem.ListViewSubItem lvsi in lvi.SubItems)
  304.                         {
  305.                             if (bFirstLvsi)
  306.                             {
  307.                                 bFirstLvsi = false;
  308.                                 sw.Write(lvsi.Text);
  309.                             }
  310.                             else
  311.                             {
  312.                                 sw.Write(lvsi.Text);
  313.                             }
  314.                         }
  315.                     }
  316.  
  317.                     sw.Close();
  318.                     fs.Close();
  319.                 }
  320.                 catch (Exception ex)
  321.                 {
  322.                     MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  323.                 }
  324.             }
  325.         }
  326.  
  327.         private void label1_Click(object sender, EventArgs e)
  328.         {
  329.  
  330.         }
  331.  
  332.         private void textBox1_TextChanged(object sender, EventArgs e)
  333.         {
  334.  
  335.         }
  336.  
  337.         private void timer1_Tick(object sender, EventArgs e)
  338.         {
  339.             if (this.progressBar1.Value < 100)
  340.             {
  341.                 this.progressBar1.Value++;
  342.                 if (this.progressBar1.Value == 100)
  343.                 {
  344.                     this.timer1.Enabled = false;
  345.                 }
  346.             }
  347.         }
  348.     }
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement