2017-09-12 11 views
1

現在アクティブなアプリケーション名(またはプロセス名)を取得しようとしましたが、Microsoft EdgeのようなアプリケーションではApplicationFrameHostという結果になります。タスクマネージャなどのアプリケーション名を取得する方法はありますか?C#を使用してWindows 10で現在のアクティブなアプリケーション名を取得

私の実際のコード:

[DllImport("user32.dll")] 
    static extern IntPtr GetForegroundWindow(); 
    [DllImport("user32.dll")] 
    static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count); 
    [DllImport("user32.dll")] 
    public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint processId); 

      private string GetActiveProcess() 
    { 

     const int nChars = 256; 
     uint processId; 
     StringBuilder Buff = new StringBuilder(nChars); 
     IntPtr handle = GetForegroundWindow(); 

     if (GetWindowText(handle, Buff, nChars) > 0) 
     { 
      GetWindowThreadProcessId(handle, out processId); 
      return Process.GetProcessById((int)processId).ProcessName; 
     } 
     return null; 
    } 
+8

次に溶液で答えを作成してください - あなたも、それを自分で受け入れることができます。タイトルに "SOLVED"を入れないでください - 受け入れられた答えはそれが解決されたことを示します。 –

+0

Victor、あなたが好きなら、私が与えた答えをコピーすることができます(編集をクリックし、すべてを選択してください)。私は次に私のものを削除します。自分の答えを自分で受け入れることもできますし、アップアップを取得することさえできます。 – halfer

答えて

1

このソリューションは、フォームアプリケーションで私のための機能になります。

 //aktivneOknoProces 
     string aktivneOknoProces = GetActiveProcess(); 

private string GetActiveProcess() 
     { 
      string app = ""; 
      var foregroundProcess = Process.GetProcessById(WinAPIFunctions.GetWindowProcessId(WinAPIFunctions.GetforegroundWindow())); 
      if (foregroundProcess.ProcessName == "ApplicationFrameHost") 
      { 
       foregroundProcess = GetRealProcess(foregroundProcess); 
      } 
      if(foregroundProcess != null) 
      { 
       app = foregroundProcess.ProcessName; 
      } 
      return app; 
     } 

     private Process GetRealProcess(Process foregroundProcess) 
     { 
      WinAPIFunctions.EnumChildWindows(foregroundProcess.MainWindowHandle, ChildWindowCallback, IntPtr.Zero); 
      return _realProcess; 
     } 

     private bool ChildWindowCallback(IntPtr hwnd, IntPtr lparam) 
     { 
      var process = Process.GetProcessById(WinAPIFunctions.GetWindowProcessId(hwnd)); 
      if (process.ProcessName != "ApplicationFrameHost") 
      { 
       _realProcess = process; 
      } 
      return true; 
     } 

     public class WinAPIFunctions 
     { 
      //Used to get Handle for Foreground Window 
      [DllImport("user32.dll", CharSet = CharSet.Auto)] 
      private static extern IntPtr GetForegroundWindow(); 

      //Used to get ID of any Window 
      [DllImport("user32.dll", CharSet = CharSet.Auto)] 
      private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); 
      public delegate bool WindowEnumProc(IntPtr hwnd, IntPtr lparam); 

      [DllImport("user32.dll")] 
      [return: MarshalAs(UnmanagedType.Bool)] 
      public static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc callback, IntPtr lParam); 

      public static int GetWindowProcessId(IntPtr hwnd) 
      { 
       int pid; 
       GetWindowThreadProcessId(hwnd, out pid); 
       return pid; 
      } 

      public static IntPtr GetforegroundWindow() 
      { 
       return GetForegroundWindow(); 
      } 
     } 
0

私は何を探していることはこれであると信じて:

Process.GetCurrentProcess().ProcessName 

GetCurrentProcess

ProcessName

あなたの名前を与える必要があること現在のプロセス

関連する問題