3

I'm looking to find some way that my Windows Small Business Server 2011 machine can auto create system events any time there is a newly created file 10 GBs and larger in a specific directory. It seems that the ideal tool to use is the File System Resource Manager, but with that I can only set hard/soft quotas for a total directory, not individually created new files. File screens don't seem to do the trick either. How can I achieve my goals with the tools that I have?

Wesley
  • 32,690
  • 9
  • 82
  • 117

1 Answers1

2

You didn't mention what 'tools' you have, so I'll give you an answer using the tools I have.

The way I would do this is implementing a simple C# program that runs in the background, maybe even a service. It would implement the FileSystemWatcher class, and subscribe to the Created event, if I read your question correctly that's what you are looking to monitor. Once the event is raised/triggered, write your event log entry.

Now, you mentioned setting quotas? You may have to expand on that and I update my answer afterward, because it's a little confusing, are you saying you want to deny people creating 10GB files in a specific folder? My next section assumes so.

After (or before) writing your event log entry, you can simply erase the file that was written, thus enabling a 'quota'. Quotas don't let you write files beyond the quota, so it's not a loss if the file was written then deleted immediately. Of course, code is worth a thousand words, so

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();
        }   

    }

}

References:

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

MDMoore313
  • 5,581
  • 6
  • 36
  • 75