我在我的代码中使用 FileSystemWatcher 来跟踪受监控目录下文件的任何更改/重命名/添加。现在,如果受监控的目录本身被删除,我需要一个通知。
关于如何实现这一目标的任何建议?
我试图在父目录(C:\temp\subfolder1在下面的示例中)添加第二个观察者并将事件过滤到受监视目录 ( C:\temp\subfolder1\subfolder2)的完整路径。
但是如果删除是在更高的目录级别完成并且我不想监视整个文件系统,这将不起作用。在下面的示例中,它也应该在删除时触发C:\temp ,而不仅仅是删除C:\temp\subfolder1.
class Program
{
static void Main(string[] args)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\temp\subfolder1\subfolder2";
watcher.EnableRaisingEvents = true;
watcher.Changed += OnChanged;
watcher.Created += OnChanged;
watcher.Deleted += OnChanged;
watcher.Renamed += OnChanged;
Console.ReadLine();
}
private static void OnChanged(object sender, FileSystemEventArgs e)
{
Console.WriteLine(e.FullPath);
}
}
Run Code Online (Sandbox Code Playgroud) 我们目前将Swashbuckle.AspNetCore用于 API 文档目的,但是在生成客户端模型 (Typescript) 时,它似乎存在一个主要缺点。
作为示例,我使用基类增强了已知的 ASP.NET 默认项目(WeatherForecast)
public class InfoEntryBase
{
public string Name { get; set; }
public string Description { get; set; }
}
public class WeatherForecast : InfoEntryBase
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string Summary { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后它将 WeatherForecast 公开为
WeatherForecast{
name string
nullable: true
description string
nullable: true
date string($date-time) …Run Code Online (Sandbox Code Playgroud) 这似乎很容易,但我认为提供优雅的解决方案很棘手。
需要将 UTC 日期时间作为字符串获取 - 但独立于 CultureInfo 设置。在某种程度上具有良好的性能 - 因为它每分钟需要几次。
因此,如果例如 Thread.CurrentThread.CurrentCulture 设置为 Thai,它将打印2020 -10-08 而不是2565 -10-08。
如上所述,寻找一个轻量级的解决方案,我不需要来回更改 Thread.CurrentThread.CurrentCulture。
我试图在下面的单元测试代码中解决这个问题。除其他外,这目前打印
阿拉伯语返回 1442-02-21T13:03:28.0393847Z
泰国返回 2563-10-08T13:03:28.3558814Z
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
StringBuilder assertionErrors = new StringBuilder();
// each culture info shall return e.g. 2020-10-08
var expected = $"{ DateTime.Now.Year}-{ DateTime.Now.Month.ToString().PadLeft(2, '0') }-{ DateTime.Now.Day.ToString().PadLeft(2, '0')}";
foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
// ARRANGE
Thread.CurrentThread.CurrentCulture = ci;
// ACT
string originalTime = GetEventTime();
// ASSERT …Run Code Online (Sandbox Code Playgroud)