在特定目录中为新创build的10 Gig文件创build系统事件

我正在寻找一些方法,使我的Windows Small Business Server 2011机器可以自动创build系统事件,只要在特定目录中有10 GB或更大的新创build的文件。 看起来理想的工具是文件系统资源pipe理器,但是我只能为总目录设置硬/软配额,而不是单独创build文件。 文件屏幕似乎也没有办法。 我如何用我拥有的工具来实现我的目标?

你没有提到你有什么“工具”,所以我会用的工具给你一个答案。

我这样做的方式是实现一个简单的C#程序,在后台运行,甚至可以是一个服务。 它会实现FileSystemWatcher类,并订阅创build的事件,如果我正确地读你的问题,那就是你正在监视。 一旦事件发生/触发,写入您的事件日志条目。

现在你提到了设定配额 ? 您可能需要扩展,然后我更新我的答案,因为这有点令人困惑,您是否要拒绝在特定文件夹中创build10GB文件的人? 我的下一部分假设如此。

在(或之前)写入事件日志条目之后,您可以简单地擦除写入的文件,从而启用“配额”。 配额不允许你写配额以外的文件,所以如果文件被写入然后立即删除,这不是一个损失。 当然,代码胜过千言万语,所以

using System; using System.IO; using System.Diagnostics; using System.Security.Permissions; public class Watcher { public static void Main() { Run(); } public static void Run() { string path = "C:\\MyDocs"; // Create a new FileSystemWatcher and set its properties. FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = path; /* Watch for changes in LastAccess and LastWrite times, and the renaming of files or directories. */ watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName; // Add event handlers. watcher.Created += new FileSystemEventHandler(OnChanged); // Begin watching. watcher.EnableRaisingEvents = true; while(true); // Do nothing but wait for files created. } // Define the event handlers. private static void OnChanged(object source, FileSystemEventArgs e) { // Specify what is done when a file is created //Test for file size FileInfo flNewFile = new FileInfo(e.FullPath); if(flNewFile.length > 10737418239) //Google says 10GB = 10737418240, so I subtracted one byte and used that as a test. { //Write to event log. EventLog elApplication = new EventLog("Application"); myLog.Source = "MyAppName"; myLog.WriteEntry("File size too big for this folder. File " + e.FullPath + " will be deleted.", EventLogEntryType.Warning); flNewFile.Delete(); } } } 

参考文献:

http://msdn.microsoft.com/en-us/library/system.io.filesystemeventargs.fullpath(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/system。 io.fileinfo.delete(v = vs.110).aspx http://msdn.microsoft.com/en-us/library/fc682h09(v=vs.110).aspx