kurocezone
kurocezone

Reputation: 3

Process.GetProcessesByName don't work with UWP

I am simply trying to recover if a process is running but impossible, an error is repeated for whatever reason... I don’t understand, after research I thought I understood that it was not possible but I can’t believe it, there is necessarily a way to recover if a program is running.

I already used Process.GetProcessesByName on WinForm and no problem... But this time with UWP i have an error... I just try to check is TeamSpeak is running

public bool isTSOpen()
{
     Process[] processesx64ts = Process.GetProcessesByName("ts3client_win64");
     Process[] processesx32ts = Process.GetProcessesByName("ts3client_win32");
     if(processesx32ts.Length == 0 && processesx64ts.Length == 0)
     {
          return true;
     }
     else
     {
          return false;
      }
}

Retrieving information about local processes is not supported on this platform.

At System.Diagnostics.NtProcessInfoHelper.GetProcessInfos(Predicate'1 machineName)
at System.Diagnostics.Process.GetProcesses(String machineName)
at System.Diagnostics.Process.GetProcessesByName(String processName, String machineName)

Upvotes: 0

Views: 1753

Answers (3)

visc
visc

Reputation: 4959

You need to run with App Diagnostics capability

Upvotes: 1

Xie Steven
Xie Steven

Reputation: 8591

@Andy's suggestions was on the right direction. The UWP has diagnostic APIs to allow an app to enumerate a list of running apps, including UWP apps, Win32 apps, system services and so on.

To make the APIs work successfully, you need to declare the appDiagnostics capability in your manifest.

<Package xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap mp rescap">
  ...

  <Capabilities>
    <rescap:Capability Name="appDiagnostics" />
  </Capabilities>
</Package>

Please note:

This is a restricted capability: If you submit an app with this capability to the Windows Store, this will trigger closer scrutiny. The app must be in the Developer Tools category, and we will examine your app to make sure that it is indeed a developer tool before approving the submission. At run time, the capability also triggers a user-consent prompt the first time any of the diagnostic APIs are called.

The user is always in control: If permission is denied, then the APIs will only return information about the current app. The prompt is only shown on first use, but the user can change his or her mind any time via the privacy pages in Settings. More information, please see UWP App Diagnostics

Back to your original question, you want to find the specific process. You first need to request permission to access diagnostics for other apps by calling AppDiagnosticInfo.RequestAccessAsync method and then, you could ProcessDiagnosticInfo.GetForProcesses method to get all running processes. At the end, you could get the specific process by its ExecutableFileName property.

I made a simple code demo for your reference:

DiagnosticAccessStatus diagnosticAccessStatus =
await AppDiagnosticInfo.RequestAccessAsync();
switch (diagnosticAccessStatus)
{
    case DiagnosticAccessStatus.Allowed:
         IReadOnlyList<ProcessDiagnosticInfo> processes = ProcessDiagnosticInfo.GetForProcesses();
         var p = processes.Where(x => x.ExecutableFileName == "ts3client_win64.exe"||x.ExecutableFileName == "ts3client_win32.exe").FirstOrDefault();
         if (p!= null)
         {
             //TODO:...
         }
         break;
    case DiagnosticAccessStatus.Limited:
         break;
}

Upvotes: 2

Andy
Andy

Reputation: 12276

You can use appdiagnosticinfo. I think you need to request user permission the first time. https://learn.microsoft.com/en-us/uwp/api/windows.system.appdiagnosticinfo

https://www.google.com/amp/s/blogs.windows.com/buildingapps/2017/06/28/uwp-app-diagnostics/amp/

Upvotes: 1

Related Questions