Hey Guys,
I want the details of the current application which is running. I know we can get thru CurrentProcess, but the problem is it always gives the name of my aplication which is running in the background
Thanks in advance
Tracy
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
TracyPosted Jan 9, 2008, 2:48 AM
AlanPosted Jan 5, 2008, 10:31 AM
If by 'current application' you mean the process which currently has the input focus, you can get this by calling the API function GetForegroundWindow() and then iterating through all the running proceesses on the machine until you find one with the same window handle. Here's some code:
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;
class Program
{
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
static void Main()
{
Console.WriteLine("You have 5 seconds to change focus to a different window");
Console.WriteLine("Otherwise, information for this process will be returned.");
Thread.Sleep(5000);
Process[] procs = Process.GetProcesses();
IntPtr hWnd = GetForegroundWindow();
foreach(Process p in procs)
{
if (hWnd == p.MainWindowHandle)
{
Console.WriteLine("\nThe process '{0}' currently has focus", p.ProcessName);
Console.WriteLine("The window title is '{0}'", p.MainWindowTitle);
}
}
Console.ReadLine();
}
}