diff --git a/MediaBrowser.ServerApplication/MainStartup.cs b/MediaBrowser.ServerApplication/MainStartup.cs index 6635cddb7b..1429824ff1 100644 --- a/MediaBrowser.ServerApplication/MainStartup.cs +++ b/MediaBrowser.ServerApplication/MainStartup.cs @@ -196,30 +196,37 @@ namespace MediaBrowser.ServerApplication private static bool IsAlreadyRunningAsService(string applicationPath) { - var serviceName = BackgroundService.GetExistingServiceName(); + try + { + var serviceName = BackgroundService.GetExistingServiceName(); - WqlObjectQuery wqlObjectQuery = new WqlObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE State = 'Running' AND Name = '{0}'", serviceName)); - ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wqlObjectQuery); - ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get(); + WqlObjectQuery wqlObjectQuery = new WqlObjectQuery(string.Format("SELECT * FROM Win32_Service WHERE State = 'Running' AND Name = '{0}'", serviceName)); + ManagementObjectSearcher managementObjectSearcher = new ManagementObjectSearcher(wqlObjectQuery); + ManagementObjectCollection managementObjectCollection = managementObjectSearcher.Get(); - foreach (ManagementObject managementObject in managementObjectCollection) - { - var obj = managementObject.GetPropertyValue("PathName"); - if (obj == null) + foreach (ManagementObject managementObject in managementObjectCollection) { - continue; - } - var path = obj.ToString(); + var obj = managementObject.GetPropertyValue("PathName"); + if (obj == null) + { + continue; + } + var path = obj.ToString(); - _logger.Info("Service path: {0}", path); - // Need to use indexOf instead of equality because the path will have the full service command line - if (path.IndexOf(applicationPath, StringComparison.OrdinalIgnoreCase) != -1) - { - _logger.Info("The windows service is already running"); - MessageBox.Show("Emby Server is already running as a Windows Service. Only one instance is allowed at a time. To run as a tray icon, shut down the Windows Service."); - return true; + _logger.Info("Service path: {0}", path); + // Need to use indexOf instead of equality because the path will have the full service command line + if (path.IndexOf(applicationPath, StringComparison.OrdinalIgnoreCase) != -1) + { + _logger.Info("The windows service is already running"); + MessageBox.Show("Emby Server is already running as a Windows Service. Only one instance is allowed at a time. To run as a tray icon, shut down the Windows Service."); + return true; + } } } + catch (COMException) + { + // Catch errors thrown due to WMI not being initialized + } return false; }