117 lines
4.1 KiB
C#
117 lines
4.1 KiB
C#
// <copyright file="Events01.cs" company="Microsoft Corporation">
|
|
// Copyright (c) 2009 Microsoft Corporation. All rights reserved.
|
|
// </copyright>
|
|
// 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;
|
|
|
|
/// <summary>
|
|
/// This class implements a cmdlet to register an event on a new instance
|
|
/// of System.IO.FileSystemWatcher.
|
|
/// <para>
|
|
/// 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.
|
|
/// </para>
|
|
/// </summary>
|
|
[Cmdlet(VerbsLifecycle.Register, "FileSystemEvent")]
|
|
public class RegisterObjectEventCommand : ObjectEventRegistrationBase
|
|
{
|
|
/// <summary>The FileSystemWatcher that exposes the events.</summary>
|
|
private FileSystemWatcher fileSystemWatcher = new FileSystemWatcher();
|
|
|
|
/// <summary>Name of the event to which the cmdlet registers.</summary>
|
|
private string eventName = null;
|
|
|
|
/// <summary>
|
|
/// Gets or sets the path that will be monitored by the FileSystemWatcher.
|
|
/// </summary>
|
|
[Parameter(Mandatory = true, Position = 0)]
|
|
public string Path
|
|
{
|
|
get
|
|
{
|
|
return this.fileSystemWatcher.Path;
|
|
}
|
|
|
|
set
|
|
{
|
|
this.fileSystemWatcher.Path = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the name of the event to which the cmdlet registers.
|
|
/// <para>
|
|
/// 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.
|
|
/// </para>
|
|
/// </summary>
|
|
[Parameter(Mandatory = true, Position = 1)]
|
|
public string EventName
|
|
{
|
|
get
|
|
{
|
|
return this.eventName;
|
|
}
|
|
|
|
set
|
|
{
|
|
this.eventName = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the filter that will be user by the FileSystemWatcher.
|
|
/// </summary>
|
|
[Parameter(Mandatory = false)]
|
|
public string Filter
|
|
{
|
|
get
|
|
{
|
|
return this.fileSystemWatcher.Filter;
|
|
}
|
|
|
|
set
|
|
{
|
|
this.fileSystemWatcher.Filter = value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Derived classes must implement this method to return the object that generates
|
|
/// the events to be monitored.
|
|
/// </summary>
|
|
/// <returns> This sample returns an instance of System.IO.FileSystemWatcher</returns>
|
|
protected override object GetSourceObject()
|
|
{
|
|
return this.fileSystemWatcher;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
/// <returns> This sample returns the event specified by the user with the -EventName parameter.</returns>
|
|
protected override string GetSourceObjectEventName()
|
|
{
|
|
return this.eventName;
|
|
}
|
|
}
|
|
}
|