C# File change/create/delete event

余生长醉 提交于 2021-01-28 11:42:51

问题


im currently getting into security stuff and trying to build a basic "access limiter" which should register if a file or folder in a specific directory (including subdirectories) is created, changed, deleted etc.

I already figured out how to do it after the action took place using FileSystemWatcher, but I want to catch the request / event before it happens to process it. I already searched a while but haven`t really found a solution yet.

If something like this is possible I would be grateful for tips or short samples / references. Thanks in advance.


回答1:


As you've stated, you can already see what happens after the fact that something has been done to a file. To my knowledge, you can't preempt file system access from .Net as this is happening on a much lower layer (right above storage). If you are trying to ensure security, you are better off focusing on NTFS/Share level security (standard permissions like Blorgbeard said).

If you really want to intercept file system calls, a file system filter driver may be what you're after but it looks very difficult.

Into to the concept: http://msdn.microsoft.com/en-us/library/windows/hardware/dn641617

Another SO answer restating the same thing (which I based the above link on): How to intercept the access to a file in a .NET Program

Alternatively, you can tailor your security app to enumerate the security settings on files/folders as a preventative measure to ensure the proper file system security settings are in place, or take corrective action if they are not. You can use the FileSecurity class to get an idea of what is available to you (quite a bit!): http://msdn.microsoft.com/en-us/library/system.security.accesscontrol.filesecurity(v=vs.110).aspx

If you want to go the audit route and get stuck on using the FileSecurity and/or DirectorySecurity class, I would post a separate question and we can tackle that.

EDIT: Some actual code on writing the filter driver, if you are so inclined (beware, no .net): http://www.codeproject.com/Articles/43586/File-System-Filter-Driver-Tutorial



来源:https://stackoverflow.com/questions/25047522/c-sharp-file-change-create-delete-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!