FileSystemWatcher - ReadOnly属性的更改事件

时间:2016-12-22 11:17:11

标签: c# .net filesystemwatcher readonly-attribute

当受监视目录中的文件的ReadOnly属性发生更改时,FileSystemMonitor看起来没有触发'Changed'事件(并且没有其他事件)。

这是我的测试代码:

    using System;
    using System.IO;

    namespace FSM
    {
        class Program
        {
            static FileSystemWatcher FolderMonitor;


            static void Main(string[] args)
            {
            FolderMonitor = new FileSystemWatcher("C:\\MyImages");
            FolderMonitor.IncludeSubdirectories = false;
            FolderMonitor.Changed += FolderMonitor_Changed; ;
            FolderMonitor.EnableRaisingEvents = true;

            Console.WriteLine("Hit any key to terminate .....");
            Console.ReadKey(true);
        }


        private static void FolderMonitor_Changed(object sender, FileSystemEventArgs e)
        {
            Console.WriteLine("****  \"" + e.Name + "\" changed.");
        }
    }

使用该代码,我收到了很多“已更改”的事件,例如如果修改时间戳已更改,但如果我正在更改任何标准属性,如ReadOnly或Hidden,则不会。

我是否遗漏了某些内容,或者我遇到了“功能”?

1 个答案:

答案 0 :(得分:5)

您需要设置import { Injectable } from '@angular/core'; import { Subject } from 'rxjs/Subject'; @Injectable() export class MissionService { // Observable string sources private missionAnnouncedSource = new Subject<string>(); private missionConfirmedSource = new Subject<string>(); // Observable string streams missionAnnounced$ = this.missionAnnouncedSource.asObservable(); missionConfirmed$ = this.missionConfirmedSource.asObservable(); // Service message commands announceMission(mission: string) { this.missionAnnouncedSource.next(mission); } confirmMission(astronaut: string) { this.missionConfirmedSource.next(astronaut); } } import { Component } from '@angular/core'; import { MissionService } from './mission.service'; @Component({ selector: 'mission-control', template: ` <h2>Mission Control</h2> <button (click)="announce()">Announce mission</button> <my-astronaut *ngFor="let astronaut of astronauts" [astronaut]="astronaut"> </my-astronaut> <h3>History</h3> <ul> <li *ngFor="let event of history">{{event}}</li> </ul> `, providers: [MissionService] }) export class MissionControlComponent { astronauts = ['Lovell', 'Swigert', 'Haise']; history: string[] = []; missions = ['Fly to the moon!', 'Fly to mars!', 'Fly to Vegas!']; nextMission = 0; constructor(private missionService: MissionService) { missionService.missionConfirmed$.subscribe( astronaut => { this.history.push(`${astronaut} confirmed the mission`); }); } announce() { let mission = this.missions[this.nextMission++]; this.missionService.announceMission(mission); this.history.push(`Mission "${mission}" announced`); if (this.nextMission >= this.missions.length) { this.nextMission = 0; } } } import { Component, Input, OnDestroy } from '@angular/core'; import { MissionService } from './mission.service'; import { Subscription } from 'rxjs/Subscription'; @Component({ selector: 'my-astronaut', template: ` <p> {{astronaut}}: <strong>{{mission}}</strong> <button (click)="confirm()" [disabled]="!announced || confirmed"> Confirm </button> </p> ` }) export class AstronautComponent implements OnDestroy { @Input() astronaut: string; mission = '<no mission announced>'; confirmed = false; announced = false; subscription: Subscription; constructor(private missionService: MissionService) { this.subscription = missionService.missionAnnounced$.subscribe( mission => { this.mission = mission; this.announced = true; this.confirmed = false; }); } confirm() { this.confirmed = true; this.missionService.confirmMission(this.astronaut); } ngOnDestroy() { // prevent memory leak when component destroyed this.subscription.unsubscribe(); } } 属性,因为这会确定要监视的更改类型。见FileSystemWatcher.NotifyFilter Property

e.g。

NotifyFilter