跟踪特定扩展名的文件

时间:2014-10-13 16:48:46

标签: c# file filesystemwatcher watch

我想跟踪用户打开的文件,并通过一个特定的扩展名选择它们。如果打开的文件具有该扩展名,那么我想将其文件路径分配给变量以进行进一步处理。示例应用程序非常需要cpu。有一种简单有效的方法吗?

1 个答案:

答案 0 :(得分:2)

系统范围的文件监控 - >打开事件(包括网络驱动器,拇指驱动器等)需要您编写FS过滤器驱动程序。

由于您可以访问该计算机,并且您肯定需要系统范围的访问,因此您只需编写一个与Powerpoint扩展相关联的简单应用程序,执行该副本,然后使用filepath作为命令行参数打开Powerpoint 。它看起来类似于以下内容:

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

希望这有帮助。