• .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("");


    }
    }
    }

  • 相关阅读:
    Python代写利用LSTM模型进行时间序列预测分析
    R语言代写使用K-Means聚类可视化纽约市WiFi访问
    BZOJ 4448: [Scoi2015]情报传递 DFS序+主席树
    BZOJ 2213: [Poi2011]Difference 细节题
    CF1268B Domino for Young 黑白染色
    BZOJ 3727: PA2014 Final Zadanie 树形DP
    BZOJ 4726: [POI2017]Sabota? 树形dp
    CF1137F Matches Are Not a Child's Play LCT+树状数组
    BZOJ 2238: Mst DFS序+KDtree
    CF1111C Creative Snap 线段树
  • 原文地址:https://www.cnblogs.com/tangyanzhi1111/p/12201480.html
Copyright © 2020-2023  润新知