简体   繁体   English

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

[英]Is it possible to get the name of current active application

User can switch active application by Alt+Tab or by clicking on their icons in TaskBar. 用户可以通过Alt + Tab或在TaskBar中单击其图标来切换活动应用程序。 Is it possible to get the name (or other unique characteristic) of current active application? 是否有可能获得当前活动应用程序的名称(或其他独特特征)?

I want to write a program which collects statistic of the applications usage. 我想编写一个程序来收集应用程序使用情况的统计信息。

The Windows API has a function called GetForegroundWindow() . Windows API有一个名为GetForegroundWindow()的函数。 You will need to use P/Invoke to call into the Win32 API. 您需要使用P / Invoke来调用Win32 API。 The P/Invoke wiki has more info for C# users. P / Invoke wiki有更多关于C#用户的信息。

See this page for an example which gets the caption (name) of the current application. 有关获取当前应用程序的标题(名称)的示例,请参阅此页面

You're looking for the API functions GetForegroundWindow and GetWindowText. 您正在寻找API函数GetForegroundWindow和GetWindowText。 There is also the GetWindowThreadProcessId function which will get the process id from the hWnd and then you can use the regular .NET classes for dealing with processes... 还有GetWindowThreadProcessId函数,它将从hWnd获取进程ID,然后您可以使用常规.NET类来处理进程...

To get the name of your c# application (several options): 要获取c#应用程序的名称(几个选项):

(first one requires you to add reference System.Windows.Forms ) (第一个要求你添加参考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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM