using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//Accede alle proprietà del progetto
using WindowsApplication1.Properties;
using System.IO;
using System.Diagnostics;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
string ora = Convert.ToString("[" + DateTime.Now.ToLongTimeString() + "] ");
string ModInt = "E' avvenuta una modifica interna a un file";
string ModInt2 = "E' avvenuta una modifica interna a un file nella 2° directory";
string created = "E' stato creato un file nella prima directory";
string created2 = "E' stato creato un file nella seconda directory";
string renamed = "E' stato rinominato un file nella prima directory";
string renamed2 = "E' stato rinominato un file nella seconda directory";
string deleted = "E' stato cancellato un file nella prima directory";
string deleted2 = "E' stato cancellato un file nella seconda directory";
string close = "Fine monitoraggio prima directory";
string close2 = "Fine monitoraggio seconda directory";
string start = "Inizio monitoraggio prima directory";
string start2 = "Inizio monitoraggio seconda directory";
string save = "Salvataggio configurazione completato";
string sott = "o sottodirectory specificata";
AboutBox1 AboutBox1
= new AboutBox1
();
public Form1()
{
InitializeComponent();
}
#region 1Directory
private void timer1_Tick(object sender, EventArgs e)
{
if (button1.Text == "Inizia monitoraggio")
{
button3.Enabled = false;
}
else
{
button3.Enabled = true;
}
if (button8.Text == "Inizia monitoraggio")
{
button9.Enabled = false;
}
else
{
button9.Enabled = true;
}
if (button1.Text == "Ferma monitoraggio" || button8.Text == "Ferma monitoraggio")
{
listBox1.Enabled = true;
}
else
{
listBox1.Enabled = false;
}
}
private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
{
{
Console.Beep();
listBox1.Items.Add(ora + ModInt + "specificata");
}
else
{
Console.Beep();
listBox1.Items.Add(ora + ModInt);
listBox1.Items.Add(sott);
}
label4.Text = "Ultima operazione registrata: " + "modifica interna";
}
private void button2_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
Path.Text = folderBrowserDialog1.SelectedPath;
}
private void button1_Click(object sender, EventArgs e)
{
//Inizia il monitoraggio
if (button1.Text == "Inizia monitoraggio")
{
{
button1.Text = "Ferma monitoraggio";
fileSystemWatcher1.Path = Path.Text;
fileSystemWatcher1.Filter = Filtro.Text;
groupBox1.Enabled = true;
listBox1.Enabled = true;
Path.Enabled = false;
Filtro.Enabled = false;
radioButton1.Enabled = false;
radioButton2.Enabled = false;
label5.Text = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
fileSystemWatcher1.EnableRaisingEvents = true;
fileSystemWatcher1.IncludeSubdirectories = true;
listBox1.Items.Add(ora + start);
}
else
{
button1.Text = "Ferma monitoraggio";
fileSystemWatcher1.Path = Path.Text;
fileSystemWatcher1.Filter = Filtro.Text;
groupBox1.Enabled = true;
listBox1.Enabled = true;
Path.Enabled = false;
Filtro.Enabled = false;
radioButton1.Enabled = false;
radioButton2.Enabled = false;
label5.Text = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
fileSystemWatcher1.EnableRaisingEvents = true;
fileSystemWatcher1.IncludeSubdirectories = false;
listBox1.Items.Add(ora + start);
}
}
else
{
//Ferma il monitoraggio
button1.Text = "Inizia monitoraggio";
fileSystemWatcher1.EnableRaisingEvents = false;
groupBox1.Enabled = false;
label5.Text = "";
listBox1.Items.Add(ora + close);
Path.Enabled = true;
Filtro.Enabled = true;
radioButton1.Enabled = true;
radioButton2.Enabled = true;
listBox1.Enabled = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.BackgroundImage = WindowsApplication1.Properties.Resources.PieroTofyit;
StreamReader sr
= new StreamReader
("confing.txt");
StreamReader SR
= new StreamReader
("confing2.txt");
string x = "";
string y = "";
if (WindowsApplication1.Properties.Settings.Default.monitoraggio == true)
{
while ((x = sr.ReadLine()) != null)
{
string[] st = x.Split('$');
Path.Text = Convert.ToString(st[0]);
Filtro.Text = Convert.ToString(st[1]);
label5.Text = st[2];
fileSystemWatcher1.Path = st[0];
fileSystemWatcher1.Filter = st[1];
if (st[3] == "Si")
{
fileSystemWatcher1.IncludeSubdirectories = true;
}
else if (st[3] == "No")
{
fileSystemWatcher1.IncludeSubdirectories = false;
}
}
groupBox1.Enabled = true;
button1.Text = "Ferma monitoraggio";
Path.Enabled = false;
Filtro.Enabled = false;
radioButton1.Enabled = false;
radioButton2.Enabled = false;
}
else
{
groupBox1.Enabled = false;
listBox1.Enabled = false;
groupBox2.Enabled = false;
}
sr.Close();
if (WindowsApplication1.Properties.Settings.Default.monitoraggio2==true)
{
while ((y = SR.ReadLine()) != null)
{
string[] ST = y.Split('$');
Path2.Text = ST[0];
Filtro2.Text = ST[1];
label8.Text = ST[2];
fileSystemWatcher2.Path = ST[0];
fileSystemWatcher2.Filter = ST[1];
if (ST[3] == "Si")
{
fileSystemWatcher2.IncludeSubdirectories = true;
groupBox2.Enabled = true;
listBox1.Enabled = true;
button8.Text = "Ferma monitoraggio";
Path2.Enabled = false;
Filtro2.Enabled = false;
Si.Enabled = false;
No.Enabled = false;
}
else if (ST[3] == "No")
{
fileSystemWatcher2.IncludeSubdirectories = false;
groupBox2.Enabled = true;
listBox1.Enabled = true;
button8.Text = "Ferma monitoraggio";
Path2.Enabled = false;
Filtro2.Enabled = false;
Si.Enabled = false;
No.Enabled = false;
}
}
}
else
{
groupBox1.Enabled = false;
listBox1.Enabled = false;
groupBox2.Enabled = false;
}
SR.Close();
}
private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
{
Console.Beep();
listBox1.Items.Add(ora + created + "sepcificata");
}
else
{
Console.Beep();
listBox1.Items.Add(ora + created);
listBox1.Items.Add(sott);
}
label4.Text = "Ultima operazione registrata: " + "creazione file";
}
private void fileSystemWatcher1_Deleted(object sender, System.IO.FileSystemEventArgs e)
{
{
Console.Beep();
listBox1.Items.Add(ora + deleted + " specificata");
}
else
{
Console.Beep();
listBox1.Items.Add(ora + deleted);
listBox1.Items.Add(sott);
}
label4.Text = "Ultima operazione registrata: " + "eliminazione file";
}
private void fileSystemWatcher1_Renamed(object sender, System.IO.RenamedEventArgs e)
{
{
Console.Beep();
listBox1.Items.Add(ora + renamed + " specificata");
}
else
{
Console.Beep();
listBox1.Items.Add(ora + renamed);
listBox1.Items.Add(sott);
}
label4.Text = "Ultima operazione registrata: " + "rinominazione file";
}
private void button3_Click(object sender, EventArgs e)
{
StreamWriter sv
= new StreamWriter
("confing.txt",
false);
//1°directory
{
sv.WriteLine(Path.Text + "$" + Filtro.Text + "$" + label5.Text + "$Si");
}
else if (radioButton2.
Checked == true)
{
sv.WriteLine(Path.Text + "$" + Filtro.Text + "$" + label5.Text + "$No");
}
sv.Close();
WindowsApplication1.Properties.Settings.Default.monitoraggio = true;
WindowsApplication1.Properties.Settings.Default.Save();
listBox1.Items.Add(ora + save);
}
private void button4_Click(object sender, EventArgs e)
{
WindowsApplication1.Properties.Settings.Default.monitoraggio = false;
WindowsApplication1.Properties.Settings.Default.monitoraggio2 = false;
WindowsApplication1.Properties.Settings.Default.Save();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
this.Visible = true;
}
private void esciToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void timer2_Tick(object sender, EventArgs e)
{
ora = Convert.ToString("[" + DateTime.Now.ToLongTimeString() + "] ");
}
#endregion
private void button7_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
Path2.Text = folderBrowserDialog1.SelectedPath;
}
private void button8_Click(object sender, EventArgs e)
{
try
{
if (button8.Text == "Inizia monitoraggio")
{
{
button8.Text = "Ferma monitoraggio";
fileSystemWatcher2.Path = Path2.Text;
fileSystemWatcher2.Filter = Filtro2.Text;
groupBox2.Enabled = true;
Path2.Enabled = false;
Filtro2.Enabled = false;
Si.Enabled = false;
No.Enabled = false;
listBox1.Enabled = true;
fileSystemWatcher2.EnableRaisingEvents = true;
label8.Text = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
fileSystemWatcher2.IncludeSubdirectories = true;
listBox1.Items.Add(ora + start2);
}
else
{
button8.Text = "Ferma monitoraggio";
fileSystemWatcher2.Path = Path2.Text;
fileSystemWatcher2.Filter = Filtro2.Text;
groupBox2.Enabled = true;
Path2.Enabled = false;
Filtro2.Enabled = false;
listBox1.Enabled = true;
Si.Enabled = false;
No.Enabled = false;
fileSystemWatcher2.EnableRaisingEvents = true;
label8.Text = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
fileSystemWatcher2.IncludeSubdirectories = false;
listBox1.Items.Add(ora + start2);
}
}
else
{
button8.Text = "Inizia monitoraggio";
fileSystemWatcher2.EnableRaisingEvents = false;
groupBox2.Enabled = false;
label8.Text = "";
listBox1.Items.Add(ora + close2);
Path2.Enabled = true;
Filtro2.Enabled = true;
Si.Enabled = false;
No.Enabled = false;
listBox1.Enabled = false;
}
}
catch (DirectoryNotFoundException)
{
MessageBox.Show("Errore, impossibile iniziare il monitoraggio", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void fileSystemWatcher2_Changed(object sender, FileSystemEventArgs e)
{
{
Console.Beep();
listBox1.Items.Add(ora + ModInt2 + " specificata");
}
else
{
Console.Beep();
listBox1.Items.Add(ora + ModInt2);
listBox1.Items.Add(sott);
}
label9.Text = "Ultima operazione registrata: " + "modifica interna";
}
private void fileSystemWatcher2_Created(object sender, FileSystemEventArgs e)
{
{
Console.Beep();
listBox1.Items.Add(ora + created2 + " specificata");
}
else
{
Console.Beep();
listBox1.Items.Add(ora + created2);
listBox1.Items.Add(sott);
}
label9.Text = "Ultima operazione registrata: " + "creazione file";
}
private void fileSystemWatcher2_Deleted(object sender, FileSystemEventArgs e)
{
{
Console.Beep();
listBox1.Items.Add(ora + deleted2 + " specificata");
}
else
{
Console.Beep();
listBox1.Items.Add(ora + deleted2);
listBox1.Items.Add(sott);
}
label9.Text = "Ultima operazione registrata: " + "eliminazione file";
}
private void fileSystemWatcher2_Renamed(object sender, RenamedEventArgs e)
{
{
Console.Beep();
listBox1.Items.Add(ora + renamed2 + " specificata");
}
else
{
Console.Beep();
listBox1.Items.Add(ora + renamed2);
listBox1.Items.Add(sott);
}
label9.Text = "Ultima operazione registrata: " + "rinominazione file";
}
private void button5_Click(object sender, EventArgs e)
{
AboutBox1.ShowDialog();
}
private void button9_Click(object sender, EventArgs e)
{
StreamWriter sw
= new StreamWriter
("confing2.txt",
false);
WindowsApplication1.Properties.Settings.Default.monitoraggio2 = true;
WindowsApplication1.Properties.Settings.Default.Save();
listBox1.Items.Add(ora + save);
//2°directory
{
sw.WriteLine(Path2.Text + "$" + Filtro2.Text + "$" + label8.Text + "$Si");
}
{
sw.WriteLine(Path2.Text + "$" + Filtro2.Text + "$" + label8.Text + "$No");
}
sw.Close();
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Process.Start("http://www.pierotofy.it/");
}
}
}