I am coding an application in C# that needs to know the current program and files a user has in focus.
I.e. I want to writ开发者_运维技巧e functions like:
string GetProgramNameCurrentlyInFocus() { ... }
string GetFilenameCurrentlyInFocus() { ... }
Which return the current program executable filename (e.g. "word.exe") or the actual official program name (e.g. "Microsoft Word 2007"), and the current file that is being viewed by the user with the current program (e.g. "C:\Users\Mat\Documents\essay.doc"), respectively.
Products like RescueTime as well as SysInternals can do these things so it is possible. I can get the title of the current program in focus, but that is not good enough as some programs do not say the program name in the title.
So, does anyone know how to code functions like that? Any tips or places I should look?
Note: I've checked out SysInternal, and despite still being free, the source code is no longer available since Microsoft bought them out.
~Mat
You can use GetForgoundWindow to get the current window in focus. I don't know a way to get what file is open.
It is possible to enumerate all open handles. This would give the name of the file (or rather all open files since a process may have more than one).
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();/
public static string GetForegroundWindowsName()
{
Process[] allproc = Process.GetProcesses();
IntPtr ii = GetForegroundWindow();
foreach (Process proc in allproc)
{
if (ii == proc.MainWindowHandle)
{
return proc.ProcessName;
}
}
return "";
}
You want GetForegroundWindow then from there you can get the main module for the application (which will generally be the application that launched it).
You might be able to determine what resources its using to find open docs, but that will be a highly manual process.
精彩评论