Tutorial :How could I prevent a folder from being created using a windows service?


I have users and processes on a web server. I'm looking to write a windows service that is capable of intercepting calls to create folders within a specific sub-directory. I've found several sources out there for reactively catching that a folder was created, but I want to create a windows service that will proactively intercept and pre-empt any unacceptable calls to create the folder.

Let me pre-empty the "why"-ers. Don't bother asking why. Suffice it to say I've been asked to do this, and I'm trying to find the simplest method of accomplishing the task in an automated way. I've already determined how to accomplish it if I let the folder get created then just follow along with my windows service and delete it (and its contents) after the fact. I really would like an event I could have the service listen to and pre-empt.


Unfortunately I don't know anywhere near enough about the how to help you, but I'm fairly sure that you'll need to either write or obtain a File System Filter Driver that can communicate with your windows service to tell it that someone has attempted to create a directory/file so that your service can make a decision for it. This way when someone/something attempts to create a file or folder that's not allowed they could be returned "Access Denied" or another Win32 error of your choice.

If you did go down the route of using a driver, I'd guess it'd still be best to do the heavy lifting of deciding if the creation/modification in the service, i.e. outside of Kernel mode.


Not sure if you're gonna find a "precreate file" intercept point. If I were you I'd push back and just lock down the security settings on your folder. If it's really just one folder and you're running on windows, you can set up security to prevent everyone from creating folders in that directory. Just be sure that's what you want to do.

Then the OS will do it without you writing any code at all.

Maybe there's a call in the security path you can inject this code into if you really have to write it yourself for some reason.


Use a FileSystemWatcher.


How are your users creating new folders? Is there a set way (IE: right click, New > Folder) or are they doing it however the Explorer UI allows? I ask because an idea would be to build an add-in for the right click feature (similar to WinRar) that is designed to create a new folder with the parameters you specify.

This would only work if they all use the exact same method to create a new folder. No need for a windows service to do it.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »