Click here to Skip to main content
15,881,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
We Have created the the eventwatcher functionality which is track the window open event its works when open the any first file with same application but when i open it the second file then that time it will not fire. for e.g. i have two excel file A.xls and B.xls when i open A.xls its works fine and after i open B.xls the it was not work.

<pre lang="c#">public void WatchForProcessStart()
        {
            string queryString =
                "SELECT TargetInstance" +
                "  FROM __InstanceCreationEvent " +
                "WITHIN  .025 " +
                " WHERE TargetInstance ISA 'Win32_Process' "
                + "   AND TargetInstance.Name like '%'";

            // The dot in the scope means use the current machine
            string scope = @"\\.\root\CIMV2";

            // Create a watcher and listen for events
            startProcWatcher = new ManagementEventWatcher(scope, queryString);
            startProcWatcher.EventArrived += ProcessStarted;
            startProcWatcher.Start();            
        }


startProcWatcher.EventArrived += ProcessStarted;

private void ProcessStarted(object sender, EventArrivedEventArgs e)
{
      string eventType = e.NewEvent.ClassPath.ClassName;
      if(eventType=="__InstanceCreationEvent")
      {
         Console.WriteLine("Event arrived");
      }

}


The above code is working when i open first excel file but when i open another excel file then that time this code is not working. 

Thanks in Advance.


What I have tried:

i have tried with __ModifiedCreationEvent but it will occur continues. please suggest what to do for it.
Posted
Comments
Richard MacCutchan 13-Jul-20 11:01am    
Are you opening both files in the same process? If so it will not fire on the second load, since that is not a new process.
[no name] 13-Jul-20 12:05pm    
If they're "your" Excel files, this might help. (The workbook "open" event) e.g. create a file entry for SystemFileWatcher, etc.

https://docs.microsoft.com/en-us/office/vba/api/excel.workbook.open

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