Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there

Basically im trying to create a background service which is activated only when user saves a file of a particular format. Do anybody know how i might be able to do so?

thanks in adv
Posted

Do you want to monitor a particular file extension, or a specific pattern in the file?
If it's a file pattern, you'll have to install a filter driver on top of the filesystem driver.
If it's a file extension, you could also use a filter driver, but the easier way is to use the FileSystemWatcher components.
Read up on this.[^]
Note that you can only monitor file systems which support monitoring. This normally includes all locally mounted file systems, and network drives where the server is running on Microsoft Technology ( So no Appleshare, NFS, consumer SAN, etc...)
 
Share this answer
 
There's several techniques to do that. One of them is to monitor _InstanceModificationEvent in WMI, here's a sample how to monitor modifications of notepad.exe:

http://technet.microsoft.com/en-us/library/ee156569.aspx[^]

/Edit MG:
The function you refer to monitors process instances, not files, and cannot be restricted to file types or formats.
Edit MG/
Please sorry me, I just gave an idea of how to do this. Here's the code sample:
C#
class Program {
    static void Main(string[] args) {
        string wql = "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'CIM_DataFile' And TargetInstance.Drive = 'D:' AND TargetInstance.Extension = 'txt'";
        ManagementEventWatcher watcher = new ManagementEventWatcher(wql);
        while (true) {
            ManagementBaseObject ev = watcher.WaitForNextEvent();
            Console.WriteLine("*** File created: {0} ***", ((ManagementBaseObject)ev["TargetInstance"])["Description"]);
            foreach (PropertyData prop in ((ManagementBaseObject) ev["TargetInstance"]).Properties) {
                Console.WriteLine(prop.Name + ": " + prop.Value);
            }
        }
    }
}
 
Share this answer
 
v3
Thank you Dmitry Vitkovsky ... ill try it out

ok yea. i tried the first solution you provided, was able to watch process but could not find the file.

ok ill try your solution too
thanks alot!
 
Share this answer
 
v2
wow thanks for the article, Michel Godfroid
i needed that
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900