当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 活动01样本

作者:精品下载站 日期:2024-12-14 02:48:10 浏览:17 分类:玩电脑

活动01样本


此示例演示如何创建允许用户注册 System.IO.FileSystemWatcher 引发的事件的 cmdlet。使用此 cmdlet,用户可以注册在特定目录下创建文件时要执行的操作。此示例派生自 Microsoft.PowerShell.Commands.ObjectEventRegistrationBase 基类。

如何使用 Visual Studio 构建示例

  1. 安装 Windows PowerShell 2.0 SDK 后,导航到 Events01 文件夹。默认位置为 C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0\Samples\sysmgmt\WindowsPowerShell\csharp\Events01。

  2. 双击解决方案 (.sln) 文件的图标。这将在 Microsoft Visual Studio 中打开示例项目。

  3. 构建菜单中,选择构建解决方案在默认的\bin\bin\debug 文件夹。

如何运行示例

  1. 创建以下模块文件夹:

    [user]\Documents\WindowsPowerShell\Modules\events01
  2. 将示例的库文件复制到模块文件夹中。

  3. 启动 Windows PowerShell。

  4. 运行以下命令将 cmdlet 加载到 Windows PowerShell 中:

    Import-Module events01
    
  5. 使用 Register-FileSystemEvent cmdlet 注册一个操作,该操作将在 TEMP 目录下创建文件时写入一条消息。

    Register-FileSystemEvent $env:temp Created -filter "*.txt" -action { Write-Host "A file was created in the TEMP directory" }
    
  6. 在 TEMP 目录下创建一个文件,并注意该操作已执行(显示消息)。

这是通过执行以下步骤得到的示例输出。

Id              Name            State      HasMoreData     Location             Command
--              ----            -----      -----------     --------             -------
1               26932870-d3b... NotStarted False                                 Write-Host "A f...

Set-Content $env:temp\test.txt "This is a test file"
A file was created in the TEMP directory

要求

此示例需要 Windows PowerShell 2.0。

示范

该示例演示了以下内容。

如何编写用于事件注册的 cmdlet

该 cmdlet 派生自 Microsoft.PowerShell.Commands.ObjectEventRegistrationBase 类,该类提供对 Register-*Event cmdlet 常用参数的支持。从 Microsoft.PowerShell.Commands.ObjectEventRegistrationBase 派生的 Cmdlet 只需定义其特定参数并重写 GetSourceObjectGetSourceObjectEventName 抽象方法。

例子

此示例演示如何注册 System.IO.FileSystemWatcher 引发的事件。

namespace Sample
{
    using System;
    using System.IO;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    using Microsoft.PowerShell.Commands;

    [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 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;
        }
    }
}

参见

  • 编写 Windows PowerShell Cmdlet

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯