//
// Copyright (c) 2009 Microsoft Corporation. All rights reserved.
//
// DISCLAIMER OF WARRANTY: The software is licensed “as-is.” You
// bear the risk of using it. Microsoft gives no express warranties,
// guarantees or conditions. You may have additional consumer rights
// under your local laws which this agreement cannot change. To the extent
// permitted under your local laws, Microsoft excludes the implied warranties
// of merchantability, fitness for a particular purpose and non-infringement.
namespace Sample
{
using System;
using System.IO;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using Microsoft.PowerShell.Commands;
///
/// This class implements a cmdlet to register an event on a new instance
/// of System.IO.FileSystemWatcher.
///
/// It derives from ObjectEventRegistrationBase, which provides support for the
/// parameters common to the Register-*Event cmdlets (e.g. -Action, -Forward,
/// -MessageData) and implements the details of event registration. Cmdlets
/// that are derived from ObjectEventRegistrationBase need only to define their
/// particular parameters and override the GetSourceObject and
/// GetSourceObjectEventName abstract methods.
///
///
[Cmdlet(VerbsLifecycle.Register, "FileSystemEvent")]
public class RegisterObjectEventCommand : ObjectEventRegistrationBase
{
/// The FileSystemWatcher that exposes the events.
private FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
/// Name of the event to which the cmdlet registers.
private string eventName = null;
///
/// Gets or sets the path that will be monitored by the FileSystemWatcher.
///
[Parameter(Mandatory = true, Position = 0)]
public string Path
{
get
{
return this.fileSystemWatcher.Path;
}
set
{
this.fileSystemWatcher.Path = value;
}
}
///
/// Gets or sets the name of the event to which the cmdlet registers.
///
/// Currently System.IO.FileSystemWatcher exposes 6 events: Changed, Created,
/// Deleted, Disposed, Error, and Renamed. Check the MSDN documentation of
/// FileSystemWatcher for details on each event.
///
///
[Parameter(Mandatory = true, Position = 1)]
public string EventName
{
get
{
return this.eventName;
}
set
{
this.eventName = value;
}
}
///
/// Gets or sets the filter that will be user by the FileSystemWatcher.
///
[Parameter(Mandatory = false)]
public string Filter
{
get
{
return this.fileSystemWatcher.Filter;
}
set
{
this.fileSystemWatcher.Filter = value;
}
}
///
/// Derived classes must implement this method to return the object that generates
/// the events to be monitored.
///
/// This sample returns an instance of System.IO.FileSystemWatcher
protected override object GetSourceObject()
{
return this.fileSystemWatcher;
}
///
/// Derived classes must implement this method to return the name of the event to
/// be monitored. This event must be exposed by the input object.
///
/// This sample returns the event specified by the user with the -EventName parameter.
protected override string GetSourceObjectEventName()
{
return this.eventName;
}
}
}