søndag 26. april 2009

Directory monitor

<------------------------->
this is a directory monior i created for work to kil and restart a app when chnges in a file is detected.
<------------------------->

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Configuration;
using System.IO;
using System.Diagnostics;
namespace TickerMonitor
{
///
/// Summary description for DirectoryMonitor.
///

public class DirectoryMonitorForm : System.Windows.Forms.Form
{
private static string Path="";
private static string Filter="";
private static bool IncludeSubs=false;
private System.IO.FileSystemWatcher FileMonitor;
private System.Windows.Forms.Label Label1;
private System.Windows.Forms.Button ButtonStart;
private System.Windows.Forms.TextBox directoryToMonitor;
///
/// Required designer variable.
///

private System.ComponentModel.Container components = null;
public DirectoryMonitorForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.FileMonitor = new System.IO.FileSystemWatcher();
this.ButtonStart = new System.Windows.Forms.Button();
this.Label1 = new System.Windows.Forms.Label();
this.directoryToMonitor = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.FileMonitor)).BeginInit();
this.SuspendLayout();
//
// FileMonitor
//
this.FileMonitor.EnableRaisingEvents = true;
this.FileMonitor.NotifyFilter = System.IO.NotifyFilters.FileName;
this.FileMonitor.SynchronizingObject = this;
this.FileMonitor.Renamed += new System.IO.RenamedEventHandler(this.FileMonitor_OnRenamed);
this.FileMonitor.Deleted += new System.IO.FileSystemEventHandler(this.FileMonitor_Changed);
this.FileMonitor.Created += new System.IO.FileSystemEventHandler(this.FileMonitor_Changed);
this.FileMonitor.Changed += new System.IO.FileSystemEventHandler(this.FileMonitor_Changed);
//
// ButtonStart
//
this.ButtonStart.BackColor = System.Drawing.Color.Gray;
this.ButtonStart.Location = new System.Drawing.Point(80, 73);
this.ButtonStart.Name = "ButtonStart";
this.ButtonStart.Size = new System.Drawing.Size(64, 23);
this.ButtonStart.TabIndex = 0;
this.ButtonStart.Text = "Start";
this.ButtonStart.UseVisualStyleBackColor = false;
this.ButtonStart.Click += new System.EventHandler(this.ButtonStart_Click);
//
// Label1
//
this.Label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Label1.Location = new System.Drawing.Point(25, 3);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(173, 24);
this.Label1.TabIndex = 2;
this.Label1.Text = "Tiker forandring monitor";
this.Label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// directoryToMonitor
//
this.directoryToMonitor.Location = new System.Drawing.Point(28, 30);
this.directoryToMonitor.Name = "directoryToMonitor";
this.directoryToMonitor.Size = new System.Drawing.Size(170, 20);
this.directoryToMonitor.TabIndex = 3;
this.directoryToMonitor.Text = "C:\\noc_beta\\Ticker\\ticker.xml";
//
// DirectoryMonitorForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(224, 108);
this.Controls.Add(this.directoryToMonitor);
this.Controls.Add(this.Label1);
this.Controls.Add(this.ButtonStart);
this.ForeColor = System.Drawing.Color.Yellow;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.Name = "DirectoryMonitorForm";
this.Text = "Ticker Monitor";
this.Load += new System.EventHandler(this.DirMon_Load);
((System.ComponentModel.ISupportInitialize)(this.FileMonitor)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.Run(new DirectoryMonitorForm());
}

private void FileMonitor_OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
string originalname = e.OldFullPath;
string renamed = e.FullPath;
MessageBox.Show("File: "+originalname+" renamed to "+renamed, e.OldName+" Renamed");
}
private void FileMonitor_Changed(object sender, System.IO.FileSystemEventArgs e)
{
string ChangeType = e.ChangeType.ToString();
////display a message box for the appropriate changetype.
//if (ChangeType=="Created")
//{
// //MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType, e.Name+" Created");
//}
//else if(ChangeType=="Deleted")
//{
// //MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType, e.Name+" Deleted");
//}
//else
if(ChangeType=="Changed")
{
// killer vist oppe
Process[] processes = Process.GetProcessesByName("NOC_Ticker");
foreach (Process p in processes)
{
//p.CloseMainWindow();
p.Kill();;
}
System.Diagnostics.Process.Start(@"C:\noc_beta\Ticker\NOC_Ticker.appref-ms");
}
}

private void ButtonStart_Click(object sender, System.EventArgs e)
{
{
//get the configuration properties from the form.
Path = directoryToMonitor.Text;
}
//Set the properties on the monitor.
FileMonitor.Path = "C:\\temp\\";
FileMonitor.Filter = Filter.ToString();
FileMonitor.IncludeSubdirectories = IncludeSubs;
FileMonitor.NotifyFilter = NotifyFilters.LastAccess NotifyFilters.LastWrite
NotifyFilters.FileName NotifyFilters.DirectoryName;
//Begin monitoring.
FileMonitor.EnableRaisingEvents = true;
}
private void ButtonStop_Click(object sender, System.EventArgs e)
{
//Begin monitoring.
FileMonitor.EnableRaisingEvents = false;
}
private void DirMon_Load(object sender, System.EventArgs e)
{
}
}
}

Ingen kommentarer:

Legg inn en kommentar