Search code examples
c#filefilesystemwatcherwatch

Tracking files of a particular extension


I want to track the files which are opened by the user, and select them by one particular extension. If the opened file has that extension, then I want to assign it's file path to a variable for further processing. Example applications are very cpu demanding. Is there an easy, efficient way to do that?


Solution

  • System wide monitoring of file-->open events (including network drives, thumb drives, etc) would require you to write a FS filter driver.

    Since you have access to the machine, and you definitely need system wide access, you could simply write a simple app that will be associated with the Powerpoint extensions, perform the copy, then open Powerpoint using the filepath as a command line argument. It would look similar to the following:

    using System;
    using System.Windows;
    using System.Diagnostics;
    using System.IO;
    
    namespace WpfApplication1
    {
        internal class MainWindow : Window
        {
            public MainWindow()
            { }
    
            [STAThread()]
            static void Main(string[] args)
            {
                if (args.Length == 0)
                {
                    // [ show error or print usage ]
                    return;
                }
    
                if (!File.Exists(args[0]))
                {
                    // [ show error or print usage ]
                    return;
                }
    
                // Perform the copy
                FileInfo target = new FileInfo(args[0]);
                string destinationFilename = string.Format("X:\\ExistingFolder\\{0}", target.Name);
                File.Copy(target.FullName, destinationFilename);
    
                // You may need to place the filename in quotes if it contains spaces
                string targetPath = string.Format("\"{0}\"", target.FullName); 
                string powerpointPath = "[FullPathToPowerpointExecutable]";
                Process powerpointInstance = Process.Start(powerpointPath, targetPath);
    
                // This solution is using a wpf windows app to avoid 
                // the flash of the console window.  However if you did
                // wish to display an accumulated list then you may choose
                // to uncomment the following block to display your UI.
    
                /*
                Application app = new Application();
                app.MainWindow = new MainWindow();
                app.MainWindow.ShowDialog();
                app.Shutdown(0);
                */
    
                Environment.Exit(0);
            }
        }
    }
    

    Hope this helps.