如何获得新打开的应用程序WPF
本文关键字:应用程序 WPF 新打开 何获得 | 更新日期: 2023-09-27 18:09:09
是否有办法获得新打开的应用程序?我有一个功能,每10秒获取一次正在运行的应用程序,把它们放在一个列表框上,如果你点击发送按钮,就把它们发送到另一个窗口。
我想要发生的是获得新打开的应用程序并将其发送到另一个窗口。例如:
前10秒,我打开了记事本和chrome,点击发送后,这两个将被发送到另一个窗口。在接下来的10秒里,我打开了另一个应用,firefox。所以我打开的应用程序现在是记事本,chrome和firefox,所以当我点击发送时,我只希望firefox被发送到另一个窗口,这样它就不会冗余。这可能吗?
这是代码,如果有什么帮助的话。
private void SendData()
{
String processID = "";
String processName = "";
String processFileName = "";
String processPath = "";
string hostName = System.Net.Dns.GetHostName();
listBox1.BeginUpdate();
try
{
for (int i = 0; i < listBox1.Items.Count; i++)
{
piis = GetAllProcessInfos();
try
{
processID = piis[i].Id.ToString();
processName = piis[i].Name.ToString();
processFileName = piis[i].FileName.ToString();
processPath = piis[i].Path.ToString();
output.Text += "'n'nSENT DATA : 'n't" + processID + "'n't" + processName + "'n't" + processFileName + "'n't" + processPath + "'n";
}
catch (Exception ex)
{
wait.Abort();
output.Text += "Error..... " + ex.StackTrace;
}
NetworkStream ns = tcpclnt.GetStream();
String data = "";
data = "--++" + " " + processID + " " + processPath + " " + processFileName + " " + hostName;
if (ns.CanWrite)
{
byte[] bf = new ASCIIEncoding().GetBytes(data);
ns.Write(bf, 0, bf.Length);
ns.Flush();
}
}
}
finally
{
listBox1.EndUpdate();
}
}
将之前发送的进程Id存储在列表中,仅当进程Id不在列表中时才发送进程信息
private List<int> listedProcesses = new List<int>();
//...
try
{
if(!listedProcesses.Contains(piis[i].Id)
{
listedProcesses.Add(piis[i].Id);
processID = piis[i].Id.ToString();
processName = piis[i].Name.ToString();
processFileName = piis[i].FileName.ToString();
processPath = piis[i].Path.ToString();
output.Text += "'n'nSENT DATA : 'n't" + processID + "'n't" + processName + "'n't" + processFileName + "'n't" + processPath + "'n";
}
}
//...
还有:当你关闭进程时,这两行将不再有效:
for (int i = 0; i < listBox1.Items.Count; i++)
{
piis = GetAllProcessInfos();
因为进程数现在可以低于列表框中的项数
为了使它们保持同步,你还必须在进程关闭时从列表框中删除项。
为了更有效(更容易)地修复它,您可以简单地清除列表框并为getallprocessinfo()返回的每个进程添加一个新项;
有一个钩子如下所述:如何在windows中钩子到应用程序和进程启动?
using System.Management;
// Run this in another thread and make sure you dispose the event watcher before exit
var start = new ManagementEventWatcher(new WqlEventQuery("SELECT * FROM Win32_ProcessStartTrace"));
start.EventArrived += new EventArrivedEventHandler(delegate (object sender, EventArrivedEventArgs e) {
console.WriteLine("Name: {0}, Command Line: {1}", e.NewEvent.Properties["ProcessName"].Value, e.NewEvent.Properties["Commandline"].Value);
});
start.Start()