Compare two ArrayList contents with C #

I have two arraylist. Namely ExistingProcess and CurrentProcess.

Arrayalist ExistingProcess contains a list of processes that started when this application started.

The CurrentProcess argualist is in the thread to run the process running on the system all the time.

Every time the arraylist currentProcess starts the current process, I want to do a comparison with the Arrayylist from ExistingProcess and show it as a message, for example,

Missing process: NotePad [If notepad is closed and the application is running] New process: MsPaint [if MSPaint is running after the application starts]

This is basically a comparison of two arraylist to find out that a new process is running and the process is closed after starting my C # application.

Hope my question is clear. Help required.

+4
source share
2 answers

First, go through the first list and remove each item from the second list. Then vice versa.

var copyOfExisting = new ArrayList( ExistingProcess ); var copyOfCurrent = new ArrayList( CurrentProcess ); foreach( var p in ExistingProcess ) copyOfCurrent.Remove( p ); foreach( var p in CurrentProcess ) copyOfExisting.Remove( p ); 

After that, the first list will contain all the missing processes, and the second - all new processes.

+3
source

You can use LINQ Except.

In addition, a difference of two sequences is created.

For samples:

http://msdn.microsoft.com/en-us/library/bb300779.aspx

http://msdn.microsoft.com/en-us/library/bb397894%28VS.90%29.aspx

Code illustrating the idea ...

 static void Main(string[] args) { ArrayList existingProcesses = new ArrayList(); existingProcesses.Add("SuperUser.exe"); existingProcesses.Add("ServerFault.exe"); existingProcesses.Add("StackApps.exe"); existingProcesses.Add("StackOverflow.exe"); ArrayList currentProcesses = new ArrayList(); currentProcesses.Add("Games.exe"); currentProcesses.Add("ServerFault.exe"); currentProcesses.Add("StackApps.exe"); currentProcesses.Add("StackOverflow.exe"); // Here only SuperUser.exe is the difference... it was closed. var closedProcesses = existingProcesses.ToArray(). Except(currentProcesses.ToArray()); // Here only Games.exe is the difference... it a new process. var newProcesses = currentProcesses.ToArray(). Except(existingProcesses.ToArray()); } 
+5
source

All Articles