Created
September 9, 2015 19:53
-
-
Save VolodyaNazarkevych/89b236c1c3373934bf1c to your computer and use it in GitHub Desktop.
for Irraaaaa)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Text; | |
using System.Windows.Forms; | |
using System.IO; | |
namespace FileChangeNotifier // Project's name. | |
{ | |
public partial class frmNotifier : Form // Partical class, base class Form. | |
{ | |
/// <summary> | |
/// Type of StringBuilder. | |
/// </summary> | |
private StringBuilder m_Sb; | |
/// <summary> | |
/// Bool veriable. | |
/// </summary> | |
private bool m_bDirty; | |
/// <summary> | |
/// Object of FileSystemWatcher. | |
/// </summary> | |
private System.IO.FileSystemWatcher m_Watcher; | |
/// <summary> | |
/// //Bool veriable. | |
/// </summary> | |
private bool m_bIsWatching; | |
/// <summary> | |
/// //Constructor of class. | |
/// </summary> | |
public frmNotifier() | |
{ | |
/// <summary> | |
/// Component initalize. | |
/// </summary> | |
InitializeComponent(); | |
m_Sb = new StringBuilder(); // Variable initialization. | |
m_bDirty = false; // Condition of object. | |
m_bIsWatching = false; // Viewing the action. | |
} | |
/// <summary> | |
/// | |
/// \parm [in] sender. | |
/// \parm [in] e. | |
private void btnWatchFile_Click(object sender, EventArgs e) | |
{ | |
if (m_bIsWatching) // Condition for watching file or directory. | |
{ | |
m_bIsWatching = false; | |
m_Watcher.EnableRaisingEvents = false; | |
m_Watcher.Dispose(); | |
btnWatchFile.BackColor = Color.LightSkyBlue; // Setting color for button. | |
btnWatchFile.Text = "Start Watching"; | |
} | |
else | |
{ | |
m_bIsWatching = true; | |
btnWatchFile.BackColor = Color.Red; //Setting color for button. | |
btnWatchFile.Text = "Stop Watching"; | |
m_Watcher = new System.IO.FileSystemWatcher(); //Initalize file system watcher. | |
if (rdbDir.Checked) // Direction; if watching directory. | |
{ | |
m_Watcher.Filter = "*.*"; // Read all formats | |
m_Watcher.Path = txtFile.Text + "\\"; // Change path. | |
} | |
else // Directory; if watching file. | |
{ | |
m_Watcher.Filter = txtFile.Text.Substring(txtFile.Text.LastIndexOf('\\') + 1); // Set file format. | |
m_Watcher.Path = txtFile.Text.Substring(0, txtFile.Text.Length - m_Watcher.Filter.Length); // Add path to the file. | |
} | |
if (chkSubFolder.Checked) // Choose Include Subfolders. | |
{ | |
m_Watcher.IncludeSubdirectories = true; | |
} | |
m_Watcher.NotifyFilter = NotifyFilters.LastAccess | // Tracking Last Access. | |
NotifyFilters.LastWrite | // Tracking Last Write. | |
NotifyFilters.FileName | // Tracking Last Changing file name. | |
NotifyFilters.DirectoryName | // Tracking Last Changing directory name. | |
NotifyFilters.Attributes | // Tracking cnanging atributes. | |
NotifyFilters.Security; // Type of tracking. | |
m_Watcher.Changed += new FileSystemEventHandler(OnChanged); // Add events Changed. | |
m_Watcher.Created += new FileSystemEventHandler(OnChanged); // Add events Created. | |
m_Watcher.Deleted += new FileSystemEventHandler(OnChanged); // Add events Deleted. | |
m_Watcher.Renamed += new RenamedEventHandler(OnRenamed); // Add events Renamed. | |
m_Watcher.EnableRaisingEvents = true; | |
} | |
} | |
// Method which call when object change was | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
/// parm [in] sender. | |
/// parm [in] e. | |
private void OnChanged(object sender, FileSystemEventArgs e) | |
{ | |
if (!m_bDirty) // Condition. | |
{ | |
m_Sb.Remove(0, m_Sb.Length); // Remove string. | |
m_Sb.Append(e.FullPath); // Add full path to pbject which was changed. | |
m_Sb.Append(" "); // Add space. | |
m_Sb.Append(e.ChangeType.ToString()); // Add changes type. | |
m_Sb.Append(" "); // Add spaces. | |
m_Sb.Append(DateTime.Now.ToString()); // Add time of change. | |
m_bDirty = true; | |
} | |
} | |
// Method which call when object renamed was. | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
/// parm [in] sender. | |
/// parm [in]e. | |
private void OnRenamed(object sender, RenamedEventArgs e) | |
{ | |
if (!m_bDirty) // Condition. | |
{ | |
m_Sb.Remove(0, m_Sb.Length); // Remove string. | |
m_Sb.Append(e.OldFullPath); // Add full path to pbject which was changed. | |
m_Sb.Append(" "); // Add space. | |
m_Sb.Append(e.ChangeType.ToString()); // Add changes type. | |
m_Sb.Append(" "); // Add spaces. | |
m_Sb.Append("to "); | |
m_Sb.Append(e.Name); // New name. | |
m_Sb.Append(" "); // Add space. | |
m_Sb.Append(DateTime.Now.ToString()); // Add time of change. | |
m_bDirty = true; | |
if (rdbFile.Checked) | |
{ | |
m_Watcher.Filter = e.Name; // File's name. | |
m_Watcher.Path = e.FullPath.Substring(0, e.FullPath.Length - m_Watcher.Filter.Length); // Path to the file. | |
} | |
} | |
} | |
// Update log's information. | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
/// parm [in] sender. | |
/// parm [in] e. | |
private void tmrEditNotify_Tick(object sender, EventArgs e) | |
{ | |
if (m_bDirty) // Condition for file. | |
{ | |
lstNotification.BeginUpdate(); // Start up date log. | |
lstNotification.Items.Add(m_Sb.ToString()); // Add new note. | |
lstNotification.EndUpdate(); // Stop up date log. | |
m_bDirty = false; | |
} | |
} | |
// File dialog function. | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
/// parm [in] sender. | |
/// parm [in] e. | |
private void btnBrowseFile_Click(object sender, EventArgs e) // Choose folder analyse. | |
{ | |
if (rdbDir.Checked) // Condition for direction. | |
{ | |
DialogResult resDialog = dlgOpenDir.ShowDialog(); // Create the dialog. | |
if (resDialog.ToString() == "OK") | |
{ | |
txtFile.Text = dlgOpenDir.SelectedPath; // Write path. | |
} | |
} | |
else // Condition for file. | |
{ | |
DialogResult resDialog = dlgOpenFile.ShowDialog(); // Create the dialog. | |
if (resDialog.ToString() == "OK") | |
{ | |
txtFile.Text = dlgOpenFile.FileName; // Write path. | |
} | |
} | |
} | |
// Save log's function | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
/// parm [in] sender. | |
/// parm [in] e. | |
private void btnLog_Click(object sender, EventArgs e) | |
{ | |
DialogResult resDialog = dlgSaveFile.ShowDialog(); // Create the dialog for save the log | |
if (resDialog.ToString() == "OK") | |
{ | |
FileInfo fi = new FileInfo(dlgSaveFile.FileName); // Create file object, initalize object in constructor which transmitted the paths name. | |
StreamWriter sw = fi.CreateText(); // Create object of writing. | |
foreach (string sItem in lstNotification.Items) // Cycle. | |
{ | |
sw.WriteLine(sItem); // Write string. | |
} | |
sw.Close(); //Close the file object. | |
} | |
} | |
// Switch's function. | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
/// parm [in] sender. | |
/// parm [in e.] | |
private void rdbFile_CheckedChanged(object sender, EventArgs e) | |
{ | |
if (rdbFile.Checked == true) // Condition for file. | |
{ | |
chkSubFolder.Enabled = false; | |
chkSubFolder.Checked = false; | |
} | |
} | |
// Switch's function | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="e"></param> | |
/// parm [in] sender. | |
/// parm [in] e. | |
private void rdbDir_CheckedChanged(object sender, EventArgs e) | |
{ | |
if (rdbDir.Checked == true) // Condition for directory. | |
{ | |
chkSubFolder.Enabled = true; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment