0

I want to write/capture the event when I double click on my file located in Windows folder. Assume that we have some txt file located in D:\MyTest\Example1.txt

When I double click on the file, the file should not open whereas it should fire an event/trigger/service to execute some tasks.

I tried with FileSystemWatcher in C#. But, when I double click on the file, the file is getting opened.(It should not).

A simple requirement for your better understanding given below.

When you double click on any txt file in Windows, it should invoke a simple batch file (Example: a.bat) to display the today's date. Conditions: When you double click on the file, the .bat file should run without opening the actual txt file.

1 Answers1

0

FileSystemWatcher monitor specific path for any file add/remove/modification and not for file execution.

There are different solution based on your needs :

  • The simplest solution is to register your program for own file extension. by double clicking on file your program got executed and you can just log it and do nothing any more or open the requested file using actual program. for example "Notepad.exe 1.txt"

  • Write a hook module and load it into explorer's process and watch for file execution but the chief drawback of this solution is third party apps or tricks to open a file that make no sense in your program. for example using CMD.exe or PowerShell.exe to open text file because you monitor specific program.

  • You can inject your hook module into all executables but it is possible to make performance issues.

  • The beset solution is to write MiniFilter driver to monitor file system but it need to good system, user mode, c++ and driver development knowledge.

  • It's possible to implement a virtual file system and control everything over it and again it need to driver development but there is good C# ports for existing libraries, the best one for C# is Dokan .Net

Describe the simplest way to implement :

  1. Write simple program without any form like this :
static void Main(string[] args)
{
    if (!args.Any())
        return;

    foreach (string argument in args)
    {
        if (Path.GetExtension(argument) != "txt")
            continue;

        if (Path.GetDirectoryName(argument) == @"D:\MyTest\")
        {
            // Call your service event here
            return;
        }

        Process.Start("Notepad.exe", argument);
    }
}
  1. Register your program for own file extension (in your case .txt) using registry configuration. Here is Microsoft documentation and maybe this topic help you.

  2. Now by double clicking on a .txt file your program got executed and path of the file(s) passed to your program as arguments. program check each file and if you want you can call something or notify your service using different mechanisms like Pipes or Messages or etc. If the file is not match you want the original application called (in my sample code "nodepad.exe") and the file open normal.

Mojtaba Tajik
  • 1,725
  • 16
  • 34