Par exemple, si l'utilisateur exécute actuellement VS2008, alors je veux la valeur VS2008.Comment puis-je déterminer le nom du processus actuellement focalisé en C#
Répondre
Je suppose que vous voulez obtenir le nom du processus propriétaire de la fenêtre actuellement focalisée. Avec certains P/Invoke:
// The GetForegroundWindow function returns a handle to the foreground window
// (the window with which the user is currently working).
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
// The GetWindowThreadProcessId function retrieves the identifier of the thread
// that created the specified window and, optionally, the identifier of the
// process that created the window.
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
// Returns the name of the process owning the foreground window.
private string GetForegroundProcessName()
{
IntPtr hwnd = GetForegroundWindow();
// The foreground window can be NULL in certain circumstances,
// such as when a window is losing activation.
if (hwnd == null)
return "Unknown";
uint pid;
GetWindowThreadProcessId(hwnd, out pid);
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
if (p.Id == pid)
return p.ProcessName;
}
return "Unknown";
}
puis-je obtenir une notification d'événement lorsque la fenêtre de premier plan est modifiée? – manishKungwani
@manishKungwani: Vous devriez poser cette question comme une nouvelle question. –
Je pensais que c'était lié, donc demandé ici, de toute façon, trouvé la solution (non vérifiée) ici: http://stackoverflow.com/questions/5876151/detect-when-theforefore-windows-changes – manishKungwani
using System;
using System.Windows;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace FGHook
{
class ForegroundTracker
{
// Delegate and imports from pinvoke.net:
delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType,
IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
[DllImport("user32.dll")]
static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr
hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess,
uint idThread, uint dwFlags);
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern Int32 GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
[DllImport("user32.dll")]
static extern bool UnhookWinEvent(IntPtr hWinEventHook);
// Constants from winuser.h
const uint EVENT_SYSTEM_FOREGROUND = 3;
const uint WINEVENT_OUTOFCONTEXT = 0;
// Need to ensure delegate is not collected while we're using it,
// storing it in a class field is simplest way to do this.
static WinEventDelegate procDelegate = new WinEventDelegate(WinEventProc);
public static void Main()
{
// Listen for foreground changes across all processes/threads on current desktop...
IntPtr hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, IntPtr.Zero,
procDelegate, 0, 0, WINEVENT_OUTOFCONTEXT);
// MessageBox provides the necessary mesage loop that SetWinEventHook requires.
MessageBox.Show("Tracking focus, close message box to exit.");
UnhookWinEvent(hhook);
}
static void WinEventProc(IntPtr hWinEventHook, uint eventType,
IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
Console.WriteLine("Foreground changed to {0:x8}", hwnd.ToInt32());
//Console.WriteLine("ObjectID changed to {0:x8}", idObject);
//Console.WriteLine("ChildID changed to {0:x8}", idChild);
GetForegroundProcessName();
}
static void GetForegroundProcessName()
{
IntPtr hwnd = GetForegroundWindow();
// The foreground window can be NULL in certain circumstances,
// such as when a window is losing activation.
if (hwnd == null)
return;
uint pid;
GetWindowThreadProcessId(hwnd, out pid);
foreach (System.Diagnostics.Process p in System.Diagnostics.Process.GetProcesses())
{
if (p.Id == pid)
{
Console.WriteLine("Pid is: {0}",pid);
Console.WriteLine("Process name is {0}",p.ProcessName);
return;
}
//return;
}
Console.WriteLine("Unknown");
}
}
}
par ce code vous serez notifié pour chaque changement de premier plan et la console imprimera le processus PID et son nom je parie réponse assez tard mais .. c'est la vie: P – user2533527
- 1. Modifier le nom du processus en C#?
- 2. Comment puis-je déterminer le nom du processus ciblé en cours et la version en C#
- 3. Déterminer le nom du compte LocalSystem en utilisant C#
- 4. C++: Obtenir le nom du processus exécutant mon application
- 5. Win32API - Comment obtenir le nom de fichier du processus à partir du handle de processus?
- 6. Comment déterminer le PID du parent d'un processus
- 7. killall sur le processus du même nom
- 8. Comment déterminer un processus "taille virtuelle" (WinXP)?
- 9. Déterminer le codage d'une chaîne en C#
- 10. Comment obtenir le nom du mois en C#?
- 11. Déterminer le nom du groupe Administrateurs local dans Vista
- 12. C# Spoof propriétaire du processus
- 13. Comment modifier le nom d'hôte en C#?
- 14. C#: Comment déterminer si ScrollBar pour un contrôle Scrollable est actuellement affiché?
- 15. Comment trouvez-vous le nom d'utilisateur/l'identité en C#
- 16. C#: trouver le processus en cours
- 17. nom du chemin et arguments des processus en cours d'exécution en utilisant le code java
- 18. Obtenez l'utilisation du processeur en cours de processus en C
- 19. Comment déterminer si le code .NET est en cours d'exécution dans un processus ASP.NET?
- 20. Comment déterminer le nom de la disposition de clavier active?
- 21. Comment déterminer un Dynamiquement Nom de la méthode en Ruby
- 22. Déterminer le temps que les processus passent Blocage/exécution
- 23. Récupère l'identité de l'utilisateur actuellement connecté, pas du compte système
- 24. Déterminer l'adresse IP d'une imprimante en C#
- 25. Obtenir le nom du fichier en PHP
- 26. Comment regarder les processus distants en C#?
- 27. Comment déterminer l'état d'un processus (si c'est un zombie)
- 28. Comment puis-je obtenir une liste de processus enfants pour un service donné en C#?
- 29. Déterminer le nom du fichier dans un sous-dossier du fichier batch
- 30. Changer la priorité du processus actuel en C
Le processus de VS 2008 est devenv.exe. Où pensez-vous que vous pouvez obtenir "VS2008"? – JRoppert