Search code examples
c#netflix

c# How to detect if Netflix is Running


I want to detect if Netflix is running. I am using a Windows Forms Application.

Netflix is a Metro App which is hosted by the process WWAHost.exe. I use the follwoing code:

Process[] ps = Process.GetProcessesByName("WWAHost");
foreach(var p in ps)
{
  if(p.MainWindowTitle == "Netflix")
  {
    return true;
  }
}

The code works for approximately 0.6 seconds after Netflix is started. After 0.6 seconds the MainWindowTitle constains an empty string. This means that it is only possible to detect Netflix right after it is started.

Update: Actually my code only works if Netflix is minimized or starting (the 0.6s second are just from the starting).

Is this a bug? Is there a better way to solve this?

my system: Win10 1809,VS2015,.Net4.5.2


Solution

  • I didn't see this behaviour of the MainWindowTitle disappearing, but here is an alternative solution. If you look in TaskManager with the Netflix application running, we can see that yes it's running as wwahost.exe, but that's given a command line which easily identifies it as the Netflix app -ServerName:Netflix.App.wwa.

    enter image description here

    So, from your C# application you can extract the process command line using WMI (you need a reference to System.Management for this).

    Here is an example:

    class Program
    {
        static void Main(string[] args)
        {
            var processes = Process
                .GetProcesses()
                .Where(a => a.IsNetflix());
    
            Console.ReadKey();
        }
    }
    
    static class Extensions
    {
        public static bool IsNetflix(this Process process)
        {
            if (process.ProcessName.IndexOf("WWAHost", StringComparison.OrdinalIgnoreCase) == -1) return false;
    
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher($"SELECT CommandLine FROM Win32_Process WHERE ProcessId = {process.Id}"))
            using (ManagementObjectCollection objects = searcher.Get())
            {
                var managementObject = objects
                    .Cast<ManagementBaseObject>()
                    .SingleOrDefault();
    
                if (managementObject == null) return false;
                return managementObject["CommandLine"].ToString().IndexOf("netflix", StringComparison.OrdinalIgnoreCase) > -1;
            }
        }
    }