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