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


}
}
}

原文地址:https://www.cnblogs.com/tangyanzhi1111/p/12201480.html