Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
DirectorySpy 1.2 - Form1.cs

Form1.cs

Caricato da:
Scarica il programma completo

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. //Accede alle proprietà del progetto
  9. using WindowsApplication1.Properties;
  10. using System.IO;
  11. using System.Diagnostics;
  12.  
  13. namespace WindowsApplication1
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         string ora = Convert.ToString("[" + DateTime.Now.ToLongTimeString() + "] ");
  18.  
  19.         string ModInt = "E' avvenuta una modifica interna a un file";
  20.         string ModInt2 = "E' avvenuta una modifica interna a un file nella 2° directory";
  21.  
  22.         string created = "E' stato creato un file nella prima directory";
  23.         string created2 = "E' stato creato un file nella seconda directory";
  24.  
  25.         string renamed = "E' stato rinominato un file nella prima directory";
  26.         string renamed2 = "E' stato rinominato un file nella seconda directory";
  27.  
  28.         string deleted = "E' stato cancellato un file nella prima directory";
  29.         string deleted2 = "E' stato cancellato un file nella seconda directory";
  30.  
  31.         string close = "Fine monitoraggio prima directory";
  32.         string close2 = "Fine monitoraggio seconda directory";
  33.         string start = "Inizio monitoraggio prima directory";
  34.         string start2 = "Inizio monitoraggio seconda directory";
  35.         string save = "Salvataggio configurazione completato";
  36.         string sott = "o sottodirectory specificata";
  37.  
  38.         AboutBox1 AboutBox1 = new AboutBox1();
  39.  
  40.         public Form1()
  41.         {
  42.             InitializeComponent();
  43.         }
  44.  
  45.         #region 1Directory
  46.         private void timer1_Tick(object sender, EventArgs e)
  47.         {
  48.             if (button1.Text == "Inizia monitoraggio")
  49.             {
  50.                 button3.Enabled = false;
  51.             }
  52.             else
  53.             {
  54.                 button3.Enabled = true;
  55.             }
  56.  
  57.             if (button8.Text == "Inizia monitoraggio")
  58.             {
  59.                 button9.Enabled = false;
  60.             }
  61.             else
  62.             {
  63.                 button9.Enabled = true;
  64.             }
  65.  
  66.             if (button1.Text == "Ferma monitoraggio" || button8.Text == "Ferma monitoraggio")
  67.             {
  68.                 listBox1.Enabled = true;
  69.             }
  70.             else
  71.             {
  72.                 listBox1.Enabled = false;
  73.             }
  74.         }
  75.  
  76.         private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
  77.         {
  78.             if (radioButton2.Checked == true)
  79.             {
  80.                 Console.Beep();
  81.                 listBox1.Items.Add(ora + ModInt + "specificata");
  82.             }
  83.             else
  84.             {
  85.                 Console.Beep();
  86.                 listBox1.Items.Add(ora + ModInt);
  87.                 listBox1.Items.Add(sott);
  88.             }
  89.             label4.Text = "Ultima operazione registrata: " + "modifica interna";
  90.         }
  91.  
  92.         private void button2_Click(object sender, EventArgs e)
  93.         {
  94.             folderBrowserDialog1.ShowDialog();
  95.             Path.Text = folderBrowserDialog1.SelectedPath;
  96.         }
  97.  
  98.         private void button1_Click(object sender, EventArgs e)
  99.         {
  100.             //Inizia il monitoraggio
  101.             if (button1.Text == "Inizia monitoraggio")
  102.             {
  103.                 if (radioButton1.Checked == true)
  104.                 {
  105.                     button1.Text = "Ferma monitoraggio";
  106.                     fileSystemWatcher1.Path = Path.Text;
  107.                     fileSystemWatcher1.Filter = Filtro.Text;
  108.                     groupBox1.Enabled = true;
  109.                     listBox1.Enabled = true;
  110.                     Path.Enabled = false;
  111.                     Filtro.Enabled = false;
  112.                     radioButton1.Enabled = false;
  113.                     radioButton2.Enabled = false;
  114.                     label5.Text = DateTime.Now.ToLongDateString() + "  " + DateTime.Now.ToLongTimeString();
  115.                     fileSystemWatcher1.EnableRaisingEvents = true;
  116.                     fileSystemWatcher1.IncludeSubdirectories = true;
  117.                     listBox1.Items.Add(ora + start);
  118.                 }
  119.                 else
  120.                 {
  121.                     button1.Text = "Ferma monitoraggio";
  122.                     fileSystemWatcher1.Path = Path.Text;
  123.                     fileSystemWatcher1.Filter = Filtro.Text;
  124.                     groupBox1.Enabled = true;
  125.                     listBox1.Enabled = true;
  126.                     Path.Enabled = false;
  127.                     Filtro.Enabled = false;
  128.                     radioButton1.Enabled = false;
  129.                     radioButton2.Enabled = false;
  130.                     label5.Text = DateTime.Now.ToLongDateString() + "  " + DateTime.Now.ToLongTimeString();
  131.                     fileSystemWatcher1.EnableRaisingEvents = true;
  132.                     fileSystemWatcher1.IncludeSubdirectories = false;
  133.                     listBox1.Items.Add(ora + start);
  134.                 }
  135.             }
  136.             else
  137.             {
  138.                 //Ferma il monitoraggio
  139.                 button1.Text = "Inizia monitoraggio";
  140.                 fileSystemWatcher1.EnableRaisingEvents = false;
  141.                 groupBox1.Enabled = false;
  142.                 label5.Text = "";
  143.                 listBox1.Items.Add(ora + close);
  144.                 Path.Enabled = true;
  145.                 Filtro.Enabled = true;
  146.                 radioButton1.Enabled = true;
  147.                 radioButton2.Enabled = true;
  148.                 listBox1.Enabled = false;
  149.             }
  150.         }
  151.  
  152.         private void Form1_Load(object sender, EventArgs e)
  153.         {
  154.             pictureBox1.BackgroundImage = WindowsApplication1.Properties.Resources.PieroTofyit;
  155.             StreamReader sr = new StreamReader("confing.txt");
  156.             StreamReader SR = new StreamReader("confing2.txt");
  157.             string x = "";
  158.             string y = "";
  159.             if (WindowsApplication1.Properties.Settings.Default.monitoraggio == true)
  160.             {
  161.                 while ((x = sr.ReadLine()) != null)
  162.                 {
  163.                     string[] st = x.Split('$');
  164.                     Path.Text = Convert.ToString(st[0]);
  165.                     Filtro.Text = Convert.ToString(st[1]);
  166.                     label5.Text = st[2];
  167.                     fileSystemWatcher1.Path = st[0];
  168.                     fileSystemWatcher1.Filter = st[1];
  169.                     if (st[3] == "Si")
  170.                     {
  171.                         radioButton1.Checked = true;
  172.                         fileSystemWatcher1.IncludeSubdirectories = true;
  173.                     }
  174.                     else if (st[3] == "No")
  175.                     {
  176.                         radioButton2.Checked = true;
  177.                         fileSystemWatcher1.IncludeSubdirectories = false;
  178.                     }
  179.                 }
  180.                 groupBox1.Enabled = true;
  181.                 button1.Text = "Ferma monitoraggio";
  182.                 Path.Enabled = false;
  183.                 Filtro.Enabled = false;
  184.                 radioButton1.Enabled = false;
  185.                 radioButton2.Enabled = false;
  186.             }
  187.             else
  188.             {
  189.                 groupBox1.Enabled = false;
  190.                 listBox1.Enabled = false;
  191.                 groupBox2.Enabled = false;
  192.             }
  193.             sr.Close();
  194.  
  195.             if (WindowsApplication1.Properties.Settings.Default.monitoraggio2==true)
  196.             {
  197.                 while ((y = SR.ReadLine()) != null)
  198.                 {
  199.                     string[] ST = y.Split('$');
  200.                     Path2.Text = ST[0];
  201.                     Filtro2.Text = ST[1];
  202.                     label8.Text = ST[2];
  203.                     fileSystemWatcher2.Path = ST[0];
  204.                     fileSystemWatcher2.Filter = ST[1];
  205.                     if (ST[3] == "Si")
  206.                     {
  207.                         Si.Checked = true;
  208.                         No.Checked = false;
  209.                         fileSystemWatcher2.IncludeSubdirectories = true;
  210.                         groupBox2.Enabled = true;
  211.                         listBox1.Enabled = true;
  212.                         button8.Text = "Ferma monitoraggio";
  213.                         Path2.Enabled = false;
  214.                         Filtro2.Enabled = false;
  215.                         Si.Enabled = false;
  216.                         No.Enabled = false;
  217.                     }
  218.                     else if (ST[3] == "No")
  219.                     {
  220.                         No.Checked = true;
  221.                         Si.Checked = false;
  222.                         fileSystemWatcher2.IncludeSubdirectories = false;
  223.                         groupBox2.Enabled = true;
  224.                         listBox1.Enabled = true;
  225.                         button8.Text = "Ferma monitoraggio";
  226.                         Path2.Enabled = false;
  227.                         Filtro2.Enabled = false;
  228.                         Si.Enabled = false;
  229.                         No.Enabled = false;
  230.                     }
  231.                 }
  232.             }
  233.             else
  234.             {
  235.                 groupBox1.Enabled = false;
  236.                 listBox1.Enabled = false;
  237.                 groupBox2.Enabled = false;
  238.             }
  239.             SR.Close();
  240.         }
  241.  
  242.         private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
  243.         {
  244.             if (radioButton2.Checked == true)
  245.             {
  246.                 Console.Beep();
  247.                 listBox1.Items.Add(ora + created + "sepcificata");
  248.             }
  249.             else
  250.             {
  251.                 Console.Beep();
  252.                 listBox1.Items.Add(ora + created);
  253.                 listBox1.Items.Add(sott);
  254.             }
  255.             label4.Text = "Ultima operazione registrata: " + "creazione file";
  256.         }
  257.  
  258.         private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
  259.         {
  260.             if (radioButton2.Checked == true)
  261.             {
  262.                 Console.Beep();
  263.                 listBox1.Items.Add(ora + deleted + " specificata");
  264.             }
  265.             else
  266.             {
  267.                 Console.Beep();
  268.                 listBox1.Items.Add(ora + deleted);
  269.                 listBox1.Items.Add(sott);
  270.             }
  271.             label4.Text = "Ultima operazione registrata: " + "eliminazione file";
  272.         }
  273.  
  274.         private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
  275.         {
  276.             if (radioButton2.Checked == true)
  277.             {
  278.                 Console.Beep();
  279.                 listBox1.Items.Add(ora + renamed + " specificata");
  280.             }
  281.             else
  282.             {
  283.                 Console.Beep();
  284.                 listBox1.Items.Add(ora + renamed);
  285.                 listBox1.Items.Add(sott);
  286.             }
  287.             label4.Text = "Ultima operazione registrata: " + "rinominazione file";
  288.         }
  289.  
  290.         private void button3_Click(object sender, EventArgs e)
  291.         {
  292.             StreamWriter sv = new StreamWriter("confing.txt", false);
  293.             //1°directory
  294.             if (radioButton1.Checked == true)
  295.             {
  296.                 sv.WriteLine(Path.Text + "$" + Filtro.Text + "$" + label5.Text + "$Si");
  297.             }
  298.             else if (radioButton2.Checked == true)
  299.             {
  300.                 sv.WriteLine(Path.Text + "$" + Filtro.Text + "$" + label5.Text + "$No");
  301.             }
  302.             sv.Close();
  303.             WindowsApplication1.Properties.Settings.Default.monitoraggio = true;
  304.             WindowsApplication1.Properties.Settings.Default.Save();
  305.             listBox1.Items.Add(ora + save);  
  306.         }
  307.  
  308.         private void button4_Click(object sender, EventArgs e)
  309.         {
  310.             WindowsApplication1.Properties.Settings.Default.monitoraggio = false;
  311.            
  312.             WindowsApplication1.Properties.Settings.Default.monitoraggio2 = false;
  313.             WindowsApplication1.Properties.Settings.Default.Save();
  314.  
  315.         }
  316.  
  317.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  318.         {
  319.             if (e.CloseReason == CloseReason.UserClosing)
  320.             {
  321.                 e.Cancel = true;
  322.                 this.Hide();
  323.             }
  324.         }
  325.  
  326.         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  327.         {
  328.             if (this.WindowState == FormWindowState.Minimized)
  329.             {
  330.                 this.WindowState = FormWindowState.Normal;
  331.             }
  332.             this.Visible = true;
  333.         }
  334.  
  335.         private void esciToolStripMenuItem_Click(object sender, EventArgs e)
  336.         {
  337.             Application.Exit();
  338.         }
  339.  
  340.         private void timer2_Tick(object sender, EventArgs e)
  341.         {
  342.             ora = Convert.ToString("[" + DateTime.Now.ToLongTimeString() + "] ");
  343.         }
  344.         #endregion
  345.  
  346.         private void button7_Click(object sender, EventArgs e)
  347.         {
  348.             folderBrowserDialog1.ShowDialog();
  349.             Path2.Text = folderBrowserDialog1.SelectedPath;
  350.         }
  351.  
  352.         private void button8_Click(object sender, EventArgs e)
  353.         {
  354.             try
  355.             {
  356.                 if (button8.Text == "Inizia monitoraggio")
  357.                 {
  358.                     if (Si.Checked == true)
  359.                     {
  360.                         button8.Text = "Ferma monitoraggio";
  361.                         fileSystemWatcher2.Path = Path2.Text;
  362.                         fileSystemWatcher2.Filter = Filtro2.Text;
  363.                         groupBox2.Enabled = true;
  364.                         Path2.Enabled = false;
  365.                         Filtro2.Enabled = false;
  366.                         Si.Enabled = false;
  367.                         No.Enabled = false;
  368.                         listBox1.Enabled = true;
  369.                         fileSystemWatcher2.EnableRaisingEvents = true;
  370.                         label8.Text = DateTime.Now.ToLongDateString() + "  " + DateTime.Now.ToLongTimeString();
  371.                         fileSystemWatcher2.IncludeSubdirectories = true;
  372.                         listBox1.Items.Add(ora + start2);
  373.                     }
  374.                     else
  375.                     {
  376.                         button8.Text = "Ferma monitoraggio";
  377.                         fileSystemWatcher2.Path = Path2.Text;
  378.                         fileSystemWatcher2.Filter = Filtro2.Text;
  379.                         groupBox2.Enabled = true;
  380.                         Path2.Enabled = false;
  381.                         Filtro2.Enabled = false;
  382.                         listBox1.Enabled = true;
  383.                         Si.Enabled = false;
  384.                         No.Enabled = false;
  385.                         fileSystemWatcher2.EnableRaisingEvents = true;
  386.                         label8.Text = DateTime.Now.ToLongDateString() + "  " + DateTime.Now.ToLongTimeString();
  387.                         fileSystemWatcher2.IncludeSubdirectories = false;
  388.                         listBox1.Items.Add(ora + start2);
  389.                     }
  390.                 }
  391.                 else
  392.                 {
  393.                     button8.Text = "Inizia monitoraggio";
  394.                     fileSystemWatcher2.EnableRaisingEvents = false;
  395.                     groupBox2.Enabled = false;
  396.                     label8.Text = "";
  397.                     listBox1.Items.Add(ora + close2);
  398.                     Path2.Enabled = true;
  399.                     Filtro2.Enabled = true;
  400.                     Si.Enabled = false;
  401.                     No.Enabled = false;
  402.                     listBox1.Enabled = false;
  403.                 }
  404.             }
  405.             catch (DirectoryNotFoundException)
  406.             {
  407.                 MessageBox.Show("Errore, impossibile iniziare il monitoraggio", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error);
  408.             }
  409.         }
  410.  
  411.         private void fileSystemWatcher2_Changed(object sender, FileSystemEventArgs e)
  412.         {
  413.             if (No.Checked == true)
  414.             {
  415.                 Console.Beep();
  416.                 listBox1.Items.Add(ora + ModInt2 + " specificata");
  417.             }
  418.             else
  419.             {
  420.                 Console.Beep();
  421.                 listBox1.Items.Add(ora + ModInt2);
  422.                 listBox1.Items.Add(sott);
  423.             }
  424.             label9.Text = "Ultima operazione registrata: " + "modifica interna";
  425.         }
  426.  
  427.         private void fileSystemWatcher2_Created(object sender, FileSystemEventArgs e)
  428.         {
  429.             if (No.Checked == true)
  430.             {
  431.                 Console.Beep();
  432.                 listBox1.Items.Add(ora + created2 + " specificata");
  433.             }
  434.             else
  435.             {
  436.                 Console.Beep();
  437.                 listBox1.Items.Add(ora + created2);
  438.                 listBox1.Items.Add(sott);
  439.             }
  440.             label9.Text = "Ultima operazione registrata: " + "creazione file";
  441.         }
  442.  
  443.         private void fileSystemWatcher2_Deleted(object sender, FileSystemEventArgs e)
  444.         {
  445.             if (No.Checked == true)
  446.             {
  447.                 Console.Beep();
  448.                 listBox1.Items.Add(ora + deleted2 + " specificata");
  449.             }
  450.             else
  451.             {
  452.                 Console.Beep();
  453.                 listBox1.Items.Add(ora + deleted2);
  454.                 listBox1.Items.Add(sott);
  455.             }
  456.             label9.Text = "Ultima operazione registrata: " + "eliminazione file";
  457.         }
  458.  
  459.         private void fileSystemWatcher2_Renamed(object sender, RenamedEventArgs e)
  460.         {
  461.             if (No.Checked == true)
  462.             {
  463.                 Console.Beep();
  464.                 listBox1.Items.Add(ora + renamed2 + " specificata");
  465.             }
  466.             else
  467.             {
  468.                 Console.Beep();
  469.                 listBox1.Items.Add(ora + renamed2);
  470.                 listBox1.Items.Add(sott);
  471.             }
  472.             label9.Text = "Ultima operazione registrata: " + "rinominazione file";
  473.         }
  474.  
  475.         private void button5_Click(object sender, EventArgs e)
  476.         {
  477.             AboutBox1.ShowDialog();
  478.         }
  479.  
  480.         private void button9_Click(object sender, EventArgs e)
  481.         {
  482.             StreamWriter sw = new StreamWriter("confing2.txt", false);
  483.  
  484.             WindowsApplication1.Properties.Settings.Default.monitoraggio2 = true;
  485.             WindowsApplication1.Properties.Settings.Default.Save();
  486.             listBox1.Items.Add(ora + save);
  487.  
  488.             //2°directory
  489.             if (Si.Checked == true)
  490.             {
  491.                 sw.WriteLine(Path2.Text + "$" + Filtro2.Text + "$" + label8.Text + "$Si");
  492.             }
  493.             else if (No.Checked == true)
  494.             {
  495.                 sw.WriteLine(Path2.Text + "$" + Filtro2.Text + "$" + label8.Text + "$No");
  496.             }
  497.             sw.Close();
  498.         }
  499.  
  500.         private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  501.         {
  502.             Process.Start("http://www.pierotofy.it/");
  503.         }
  504.     }    
  505. }