使用C#通知网站asp.net中目录的更改

Notify the change in directory in a website asp.net with c#
2020-09-18
  •  译文(汉语)
  •  原文(英语)

如果要更改特定服务器目录(包括子目录)的内容(添加或删除或修改文件),我想为网站实现OBSERVER模式以引发事件.此外,无法通过网站url中的映射访问目录.

解决过程1

首先,如果要检查文件夹内部是否已添加删除项目,则可以使用:

List<DirectoryInfo> allSubDirectories = new List<DirectoryInfo>();

DirectoryInfo myDir = new DirectoryInfo("/Path/To/Base/Directory/Here");

foreach(DirectoryInfo di in myDir.GetDirectories())
{
    allSubDirectories.Add(di);
}

现在,allSubDirectories保存基本目录中所有目录的信息,并且您可以以相同的方式检索内部目录.

因此,现在我们应该检查是否添加或删除了某些内容,如下所示:

int numberOfFiles = someDir.GetFiles().Length; // This will retrieve how much files are there at the start

然后,您可以轻松检查长度是否不同于某些DateTime或取决于您的需要

现在要检查文件是否已修改,请在此处查看示例

https://msdn.microsoft.com/zh-CN/library/system.io.filesystemwatcher.changed(v=vs.110).aspx

类FileSystemWatcher具有事件Changed wich的事件将提供有关文件是否已打开,更改,大小更改,文件名更改和其他信息的信息.

I want to implement OBSERVER pattern for a website to raise a event if the content of a particular server directory (including sub directory) is changed (files added or deleted or modified). Moreover the directory can't be accessed via mapping in the website url.

Solutions1

Firstly if you want to check if inside a folder has been added or deleted an item you might use:

List<DirectoryInfo> allSubDirectories = new List<DirectoryInfo>();

DirectoryInfo myDir = new DirectoryInfo("/Path/To/Base/Directory/Here");

foreach(DirectoryInfo di in myDir.GetDirectories())
{
    allSubDirectories.Add(di);
}

Now allSubDirectories holds info of all directories inside the base directory, and in the same manner you can retrieve the inner directories.

So now we should check if something is added or removed like so:

int numberOfFiles = someDir.GetFiles().Length; // This will retrieve how much files are there at the start

Then you can easly check if the length is different from some DateTime or depending on your needs

Now to check if a file has been modified, check the examples here

https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.changed(v=vs.110).aspx

The class FileSystemWatcher has event Changed wich will give information if the file was opened, changed, modified size, modified file name and other info.

转载于:https://stackoverflow.com/questions/32348360/notify-the-change-in-directory-in-a-website-asp-net-with-c-sharp

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入