我正在使用以下法典来研究启动的任何新进程。 这一职能正在read。
I need to log in the process name started. For which I use two arraylist. On one arraylist i store all the process names before it starts the thread and the other arraylist, I fill the current process inside the thread and compare two arraylist to find the new process.
The problem now is, since the string to write to the log file is in for each loop, I see repeated process names in the log file. I want it to be logged only once. How can I solve this?
class ProcessMonitor
{
public static ArrayList ExistingProcess = new ArrayList();
public static void Monitor()
{
existingProcesses = GetExistingProcess();
while (true)
{
ArrayList currentProcesses = new ArrayList();
currentProcesses = GetCurrentProcess();
ArrayList NewApps = new ArrayList(GetCurrentProcess());
foreach (var p in ExistingProcess)
{
NewApps.Remove(p);
}
string str = "";
foreach (string NewApp in NewApps)
{
str += "Launched ProcessName/ID : " + NewApp + "/" + System.Diagnostics.Process.GetProcessesByName(NewApp)[0].Id.ToString() + Environment.NewLine;
}
if(str!="")
{
Log.Info(str);
}
}
}
public static ArrayList GetExistingProcess()
{
Process[] processlist = Process.GetProcesses();
foreach (Process Proc in processlist)
{
ExistingProcess.Add(Proc.ProcessName);
}
return ExistingProcess;
}
public static ArrayList GetCurrentProcess()
{
ArrayList CurrentProcesses = new ArrayList();
Process[] processlist = Process.GetProcesses();
foreach (Process Proc in processlist)
{
CurrentProcesses.Add(Proc.ProcessName);
}
return CurrentProcesses;
}
}