如何使用FileSystemWatcher和Incoming Webhook转发通知



我正在努力做到这一点,以便每当监视文件夹中有更改时,我都会收到团队的通知。

我用FileSystemWatcher编写了监控代码,但当需要通知团队更改时,我什么也没得到。

你能帮我吗?

以下是我迄今为止创建的代码:

static async Task Main(string[] args)
{
var Path = args[0];
using var watcher = new FileSystemWatcher(Path);
watcher.NotifyFilter = NotifyFilters.Attributes
| NotifyFilters.CreationTime
| NotifyFilters.DirectoryName
| NotifyFilters.FileName
| NotifyFilters.LastAccess
| NotifyFilters.LastWrite
| NotifyFilters.Security
| NotifyFilters.Size;
watcher.Changed += OnChanged;
watcher.Created += OnCreated;
watcher.Deleted += OnDeleted;
watcher.Renamed += OnRenamed;
watcher.Error += OnError;
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "URL_Incoming Webhook"))
{
request.Content = new StringContent("{'text':$Created}");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
HttpResponseMessage response = await httpClient.SendAsync(request);
}
}
watcher.Filter = "";
watcher.IncludeSubdirectories = false;
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
private static void OnChanged(object sender, FileSystemEventArgs e)
{
if (e.ChangeType != WatcherChangeTypes.Changed)
{
return;
}
Console.WriteLine($"Changed: {e.FullPath}");
}
private static void OnCreated(object sender, FileSystemEventArgs e)
{
string value = $"Created: {e.FullPath}";
Console.WriteLine(value);
}
private static void OnDeleted(object sender, FileSystemEventArgs e) =>
Console.WriteLine($"Deleted: {e.FullPath}");
private static void OnRenamed(object sender, RenamedEventArgs e)
{
Console.WriteLine($"Renamed:");
Console.WriteLine($"    Old: {e.OldFullPath}");
Console.WriteLine($"    New: {e.FullPath}");
}
private static void OnError(object sender, ErrorEventArgs e) =>
PrintException(e.GetException());

private static void PrintException(Exception? ex)
{
if (ex != null)
{
Console.WriteLine($"Message: {ex.Message}");
Console.WriteLine("Stacktrace:");
Console.WriteLine(ex.StackTrace);
Console.WriteLine();
PrintException(ex.InnerException);
}
}

我不知道这是否是最好的方法,但我愿意接受建议,谢谢。

注意您可以做一些工作来改进此代码,但为了回答所问的问题而不陷入切线,我将保持原样(几乎(。

可能我错过了什么,但我看不出当你的活动被解雇时,你在哪里可以向团队发送请求。

首先,将您的http请求放入一个方法中

private static async Task sendRequestToTeams(string Text)
{
try
{
using (var httpClient = new HttpClient())
{
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "URL_Incoming Webhook"))
{
request.Content = new StringContent($"{{"text":"{Text}"}}");
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
HttpResponseMessage response = await httpClient.SendAsync(request);
}
}
}
catch (Exception error)
{
Console.WriteLine(error.ToString());
}
}

然后你可以在你的每一次活动中都这样称呼它:

private static void OnDeleted(object sender, FileSystemEventArgs e) 
{
sendRequestToTeams($"Deleted: {e.FullPath}")
.GetAwaiter().GetResult(); // allows you to call async from non async code
Console.WriteLine($"Deleted: {e.FullPath}");
}

相关内容

  • 没有找到相关文章

最新更新