用戶可以通過Alt + Tab或單擊任務欄中的圖標來切換活動應用程序。是否有可能獲得當前活動應用程序的名稱(或其他獨特的特徵)?是否可以獲取當前活動應用程序的名稱
我想寫一個收集統計應用程序使用情況的程序。
用戶可以通過Alt + Tab或單擊任務欄中的圖標來切換活動應用程序。是否有可能獲得當前活動應用程序的名稱(或其他獨特的特徵)?是否可以獲取當前活動應用程序的名稱
我想寫一個收集統計應用程序使用情況的程序。
Windows API有一個叫做GetForegroundWindow()的函數。您將需要使用P/Invoke調用Win32 API。 P/Invoke wiki爲C#用戶提供more info。
有關獲取當前應用程序的標題(名稱)的示例,請參閱this page。
您正在尋找API函數GetForegroundWindow和GetWindowText。還有GetWindowThreadProcessId函數,它將從hWnd獲取進程ID,然後您可以使用常規.NET類來處理進程...
要獲取您的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;