Is it possible to get the name of current active application

本小妞迷上赌 提交于 2019-12-21 22:26:01

问题


User can switch active application by Alt+Tab or by clicking on their icons in 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.


回答1:


The Windows API has a function called GetForegroundWindow(). You will need to use P/Invoke to call into the Win32 API. The P/Invoke wiki has more info for C# users.

See this page for an example which gets the caption (name) of the current application.




回答2:


You're looking for the API functions GetForegroundWindow and 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...




回答3:


To get the name of your c# application (several options):

(first one requires you to add reference 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;


来源:https://stackoverflow.com/questions/2483537/is-it-possible-to-get-the-name-of-current-active-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!