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
Key Logger
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

AlanRank #669 · 3.6K reputationPosted Jan 5, 2008, 5:31 AM EST
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();
}
}
Tracy17 reputationPosted Jan 8, 2008, 9:48 PM EST