zoukankan      html  css  js  c++  java
  • .Net Core监控文件改动

    static void Main(string[] args)
    {
    IFileProvider fileProvider = new PhysicalFileProvider(Environment.CurrentDirectory);
    Action<object> callback = null;
    IDisposable register = null;
    callback = _ =>
    {
    register.Dispose();
    LoadFileAsync(fileProvider);
    register =fileProvider.Watch("a.txt").RegisterChangeCallback(callback, null);
    };
    register = fileProvider.Watch("a.txt").RegisterChangeCallback(callback, null);
    Console.ReadLine();
    }

    public static async void LoadFileAsync(IFileProvider fileProvider)
    {
    Stream stream = fileProvider.GetFileInfo("a.txt").CreateReadStream();
    {
    byte[] buffer = new byte[stream.Length];
    await stream.ReadAsync(buffer, 0, buffer.Length);
    stream.Close();

    Console.WriteLine(Encoding.ASCII.GetString(buffer));
    }
    }

    源码分析:

    using Microsoft.Extensions.FileSystemGlobbing;
    using Microsoft.Extensions.Primitives;
    using System;
    using System.IO;
    using System.Threading;

    namespace ConsoleApp14
    {
    class physical
    {
    private readonly Func<physicalwatch> _fileWatcherFactory;
    private physicalwatch _fileWatcher;
    private bool _fileWatcherInitialized;
    private object _fileWatcherLock = new object();

    string Path = null;
    public physical(string path)
    {
    Path = path;
    _fileWatcherFactory =()=> Createfilewatch();
    }

    public physicalwatch Createfilewatch()
    {
    return new physicalwatch(Path, new FileSystemWatcher(Path));
    }

    public physicalwatch FileWatcher
    {
    get
    {
    return LazyInitializer.EnsureInitialized(ref _fileWatcher, ref _fileWatcherInitialized, ref _fileWatcherLock, _fileWatcherFactory);
    }
    set
    {
    //Debug.Assert(!_fileWatcherInitialized);

    _fileWatcherInitialized = true;
    _fileWatcher = value;
    }
    }
    }

    class physicalwatch
    {
    private readonly FileSystemWatcher _fileWatcher;

    public physicalwatch(string path ,FileSystemWatcher fh)
    {
    _fileWatcher = fh;
    _fileWatcher.IncludeSubdirectories = true;
    _fileWatcher.Created += OnChanged;
    _fileWatcher.Changed += OnChanged;
    _fileWatcher.Renamed += OnRenamed;
    _fileWatcher.Deleted += OnChanged;
    _fileWatcher.Error += OnError;
    }

    private void OnError(object sender, ErrorEventArgs e)
    {
    throw new NotImplementedException();
    }

    private void OnRenamed(object sender, RenamedEventArgs e)
    {
    throw new NotImplementedException();
    }

    private void OnChanged(object sender, FileSystemEventArgs e)
    {
    throw new NotImplementedException();
    }
    }
    public class CancellationChangeToken : IChangeToken
    {
    /// <summary>
    /// Initializes a new instance of <see cref="CancellationChangeToken"/>.
    /// </summary>
    /// <param name="cancellationToken">The <see cref="CancellationToken"/>.</param>
    public CancellationChangeToken(CancellationToken cancellationToken)
    {
    Token = cancellationToken;
    }
    private CancellationToken Token { get; }

    bool IChangeToken.HasChanged => throw new NotImplementedException();

    bool IChangeToken.ActiveChangeCallbacks => throw new NotImplementedException();

    IDisposable IChangeToken.RegisterChangeCallback(Action<object> callback, object state)
    {
    throw new NotImplementedException();
    }
    }
    public struct ChangeTokenInfo
    {
    public ChangeTokenInfo(
    CancellationTokenSource tokenSource,
    CancellationChangeToken changeToken)
    : this(tokenSource, changeToken, matcher: null)
    {
    }

    public ChangeTokenInfo(
    CancellationTokenSource tokenSource,
    CancellationChangeToken changeToken,
    Matcher matcher)
    {
    TokenSource = tokenSource;
    ChangeToken = changeToken;
    Matcher = matcher;
    }

    public CancellationTokenSource TokenSource { get; }

    public CancellationChangeToken ChangeToken { get; }

    public Matcher Matcher { get; }
    }

    class Program
    {
    static void Main(string[] args)
    {
    IChangeToken changeToken;
    ChangeTokenInfo tokenInfo;
    var cancellationTokenSource = new CancellationTokenSource();
    var cancellationChangeToken = new CancellationChangeToken(cancellationTokenSource.Token);
    tokenInfo = new ChangeTokenInfo(cancellationTokenSource, cancellationChangeToken);

    changeToken = tokenInfo.ChangeToken;

    changeToken.RegisterChangeCallback()

    physical pl = new physical("");


    }
    }
    }

  • 相关阅读:
    Windows自动更新所需要连接的网站列表
    DFX 9.303 for QQMusic 2010
    在VPC 2007 SP1中安装Ubuntu 10.04 desktop (完成)
    穷人把钱存入银行,实际上是补贴富人。
    清理Windows右下角图标
    阿里镜像pull 加速器
    k8s 安装flannel网络插件
    k8s pull.sh
    kubeadm1.10.00 安装k8s集群
    虚拟机vmware centos7 扩展磁盘空间
  • 原文地址:https://www.cnblogs.com/tangyanzhi1111/p/12201480.html
Copyright © 2011-2022 走看看