using System;
using System.IO;
using System.Windows.Forms;
namespace SystemFileWatcherTestInC_
{
public partial class Form1 : Form
{
String gcStartDir = (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
String gcWorkingDirRoot = "";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
gcStartDir = gcStartDir.Replace("bin\\Debug", "");
dlgDirSelect.SelectedPath = gcStartDir;
txtDirSelect.Text = gcStartDir;
cboExtFilter.Items.Add("*.TXT");
cboExtFilter.Items.Add("*.XML");
cboExtFilter.Items.Add("*.DOCX");
cboExtFilter.Items.Add("*.DOC");
cboExtFilter.Items.Add("*.LOG");
}
private void btnDirSelect_Click(object sender, EventArgs e)
{
dlgDirSelect.SelectedPath = txtDirSelect.Text;
DialogResult liAns = dlgDirSelect.ShowDialog();
if (liAns != DialogResult.OK) { return; }
else
{
txtDirSelect.Text = dlgDirSelect.SelectedPath;
gcWorkingDirRoot = txtDirSelect.Text;
}
}
private void btnStart_Click(object sender, EventArgs e)
{
var watcher = new FileSystemWatcher(txtDirSelect.Text);
watcher.Filter = cboExtFilter.Text;
watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
watcher.Changed += OnChanged;
watcher.Created += OnCreated;
watcher.Deleted += OnDeleted;
watcher.Renamed += OnRenamed;
watcher.Error += OnError;
}
private void OnChanged(object sender, FileSystemEventArgs e) // static
{
if (e.ChangeType != WatcherChangeTypes.Changed) { return; }
else {rtbReport.Text += "Changed: " + e.FullPath; } // Console.WriteLine($");
}
private void OnCreated(object sender, FileSystemEventArgs e) // static
{
if (e.ChangeType != WatcherChangeTypes.Created) { return; }
else { rtbReport.Text += "Created: " + e.FullPath; } // Console.WriteLine($");
}
private void OnDeleted(object sender, FileSystemEventArgs e) // static
{
if (e.ChangeType != WatcherChangeTypes.Deleted) { return; }
else { rtbReport.Text += "Deleted: " + e.FullPath; } // Console.WriteLine($");
}
private void OnRenamed(object sender, FileSystemEventArgs e) // static
{
if (e.ChangeType != WatcherChangeTypes.Renamed) { return; }
else { rtbReport.Text += "Renamed: " + e.FullPath + "\r\n"; } // Console.WriteLine($");
// + " Old: " + e.OldFullPath + "\r\n" }
private static void OnError(object sender, ErrorEventArgs e) =>
PrintException(e.GetException());
private static void PrintException(Exception ex)
{
if (ex != null) {
string lsErrMsg = "Error: " + ex.Message + "\n";
// Console.WriteLine($"Message: {ex.Message}");
lsErrMsg += ("Stasck Trace: " + ex.StackTrace + "\n"); // Console.WriteLine("Stacktrace:");
// Console.WriteLine(ex.StackTrace);
// Console.WriteLine();
// PrintException(ex.InnerException);
}
}
}
}
// Form Designer
namespace SystemFileWatcherTestInC_
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.btnExit = new System.Windows.Forms.Button();
this.dlgDirSelect = new System.Windows.Forms.FolderBrowserDialog();
this.txtDirSelect = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.btnDirSelect = new System.Windows.Forms.Button();
this.rtbReport = new System.Windows.Forms.RichTextBox();
this.btnStart = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.cboExtFilter = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// btnExit
//
this.btnExit.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.btnExit.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnExit.Location = new System.Drawing.Point(988, 503);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 24);
this.btnExit.TabIndex = 0;
this.btnExit.Text = "E&xit";
this.btnExit.UseVisualStyleBackColor = true;
this.btnExit.Click += new System.EventHandler(this.button1_Click);
//
// txtDirSelect
//
this.txtDirSelect.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.txtDirSelect.Font = new System.Drawing.Font("Arial", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtDirSelect.Location = new System.Drawing.Point(93, 22);
this.txtDirSelect.Name = "txtDirSelect";
this.txtDirSelect.Size = new System.Drawing.Size(930, 23);
this.txtDirSelect.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(12, 25);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(69, 16);
this.label1.TabIndex = 2;
this.label1.Text = "Select Dir";
//
// btnDirSelect
//
this.btnDirSelect.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnDirSelect.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnDirSelect.Location = new System.Drawing.Point(1031, 25);
this.btnDirSelect.Name = "btnDirSelect";
this.btnDirSelect.Size = new System.Drawing.Size(41, 23);
this.btnDirSelect.TabIndex = 3;
this.btnDirSelect.Text = "...";
this.btnDirSelect.UseVisualStyleBackColor = true;
this.btnDirSelect.Click += new System.EventHandler(this.btnDirSelect_Click);
//
// rtbReport
//
this.rtbReport.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.rtbReport.Location = new System.Drawing.Point(29, 54);
this.rtbReport.Name = "rtbReport";
this.rtbReport.Size = new System.Drawing.Size(1043, 434);
this.rtbReport.TabIndex = 4;
this.rtbReport.Text = "";
//
// btnStart
//
this.btnStart.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnStart.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnStart.Location = new System.Drawing.Point(549, 503);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 24);
this.btnStart.TabIndex = 5;
this.btnStart.Text = "&Go";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(17, 503);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(84, 16);
this.label2.TabIndex = 6;
this.label2.Text = "Select Filter";
//
// cboExtFilter
//
this.cboExtFilter.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.cboExtFilter.FormattingEnabled = true;
this.cboExtFilter.Location = new System.Drawing.Point(126, 503);
this.cboExtFilter.Name = "cboExtFilter";
this.cboExtFilter.Size = new System.Drawing.Size(384, 24);
this.cboExtFilter.TabIndex = 7;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1084, 540);
this.Controls.Add(this.cboExtFilter);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.rtbReport);
this.Controls.Add(this.btnDirSelect);
this.Controls.Add(this.label1);
this.Controls.Add(this.txtDirSelect);
this.Controls.Add(this.btnExit);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.FolderBrowserDialog dlgDirSelect;
private System.Windows.Forms.TextBox txtDirSelect;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnDirSelect;
private System.Windows.Forms.RichTextBox rtbReport;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox cboExtFilter;
}
}