是否可以获取当前活动应用程序的名称

时间:2010-03-20 15:12:30

标签: c# .net windows pinvoke

用户可以通过Alt + Tab或在TaskBar中单击其图标来切换活动应用程序。是否有可能获得当前活动应用程序的名称(或其他独特特征)?

我想编写一个收集应用程序使用情况统计信息的程序。

3 个答案:

答案 0 :(得分:6)

Windows API有一个名为GetForegroundWindow()的函数。您需要使用P / Invoke来调用Win32 API。 P / Invoke wiki对C#用户有more info

有关获取当前应用程序标题(名称)的示例,请参阅this page

答案 1 :(得分:1)

您正在寻找API函数GetForegroundWindow和GetWindowText。还有GetWindowThreadProcessId函数,它将从hWnd获取进程ID,然后您可以使用常规.NET类来处理进程...

答案 2 :(得分:1)

获取c#应用程序的名称(多个选项):

(第一个要求您添加参考System.Windows.Forms

string name1 = System.Windows.Forms.Application.ExecutablePath;
string name2 = System.IO.Path.GetFullPath(System.Reflection.Assembly.GetExecutingAssembly().Location
string name3 = System.IO.Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string name4 = Environment.GetCommandLineArgs()[0];
string name5 = System.IO.Path.GetFileName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
string name6 = System.Reflection.Assembly.GetEntryAssembly().CodeBase;
string name7 = System.Reflection.Assembly.GetEntryAssembly().FullName;