question

MiPakTeh-6272 avatar image
0 Votes"
MiPakTeh-6272 asked Viorel-1 answered

ManagementEventWacther and FilesystemWacther

Hi All,

Test if managementEvenWacther can start FilesystemWacther from Class. It seem the method "Bulk" not fired.
can somebody show the true code and something to add.

Thank.

Form code;

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.Management;
 using System.Diagnostics;
    
 namespace Mag_
 {
     public partial class Form1 : Form
     {
         private Step_ NameOfClass;
    
         string query_ = "SELECT * FROM __InstanceCreationEvent " + "WITHIN 1 WHERE " + "TargetInstance isa \"Win32_Process\" and TargetInstance.Name='Second.exe'";
         string Scope_ = "root\\CIMV2";
    
         public Form1()
         {
             InitializeComponent();
    
             ManagementEventWatcher Eye_ = new ManagementEventWatcher(Scope_, query_);
             Eye_.EventArrived += new EventArrivedEventHandler(this.Bulk);
    
         }
         private void Bulk(object sender, EventArrivedEventArgs e)
         {
             NameOfClass = new Step_(this);
             NameOfClass.First();
    
    
         }
         public void DoSomthing(string message)
         {
             this.Invoke((MethodInvoker)delegate ()
             {
                 listBox1.Items.Add(message);
             });
         }
         private void button1_Click(object sender, EventArgs e)
         {
    
         }
         private void Form1_Load(object sender, EventArgs e)
         {
    
         }
    
    
     }
 }

Class code;

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.IO;
    
 namespace Mag_
 {
     class Step_
     {
         Mag_.Form1 Formed;
    
     public Step_(Form1 Formed)
     {
         this.Formed = Formed;
     }
    
         List<string> filesAdded = new List<string>();
    
         public void First()
         {
             string path = @"C:\";
    
             var watcher = new FileSystemWatcher();
    
             watcher.NotifyFilter = NotifyFilters.Attributes
                                  | NotifyFilters.CreationTime
                                  | NotifyFilters.DirectoryName
                                  | NotifyFilters.FileName
                                  | NotifyFilters.LastAccess
                                  | NotifyFilters.LastWrite
                                  | NotifyFilters.Security
                                  | NotifyFilters.Size;
    
             watcher.Created += new FileSystemEventHandler(OnCreated);
             watcher.Path = path;
             watcher.Filter = "*.*";
             watcher.IncludeSubdirectories = true;
             watcher.EnableRaisingEvents = true;
         }
    
         public void OnCreated(object sender, FileSystemEventArgs e)
         {
             filesAdded.Add(e.FullPath);
    
             foreach (var sView in filesAdded)
             {
                 string message = sView.ToString();
                 Formed.DoSomthing(message);
             }
         }
    
    
     }
 }
dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered

To activate the ManagementEventWatcher, try adding this line:

Eye_.Start( );

And maybe declare Eye_ as a member of your class (instead of local variable).


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.