Пример #1
0
        public Process GetProcessForFilePath(string filePath)
        {
            var     processes         = _processLogic.GetProcesses();
            Process result            = null;
            var     matchingProcesses = new List <Process>();

            foreach (var process in processes)
            {
                try
                {
                    var fileName = process.MainModule.FileName;
                    if (fileName == filePath)
                    {
                        matchingProcesses.Add(process);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogDebug($"Failed to access process {process.Id}", ex);
                }
            }

            if (matchingProcesses.Count <= 0)
            {
                _logger.LogError("Failed to get process for filepath");
            }
            else if (matchingProcesses.Count > 1)
            {
                throw new Exception("Multiple processes found for filepath!");
            }
            else
            {
                result = matchingProcesses.First();
            }

            if (result == null)
            {
                throw new Exception("Could not find process");
            }
            return(result);
        }