Exemple #1
0
        private IEnumerable <SystemInfoDto> GetLogicalDriveProperties(ManagementObject managementObject)
        {
            var result = new List <SystemInfoDto>();

            result.TryAdd <string>(SystemInfoCategory.Drives, managementObject, "Description");
            result.TryAdd <uint>(SystemInfoCategory.Drives, managementObject, "DriveType", u =>
            {
                var driveType = (DriveType)u;
                return(new TranslatedTextValueDto($"Drives.DriveType.{driveType}"));
            });

            result.TryAdd <string>(SystemInfoCategory.Drives, managementObject, "FileSystem");
            result.TryAdd <ulong>(SystemInfoCategory.Drives, managementObject, "FreeSpace", i => new DataSizeValueDto((long)i));
            result.TryAdd <ulong>(SystemInfoCategory.Drives, managementObject, "Size", i => new DataSizeValueDto((long)i));
            result.TryAdd <string>(SystemInfoCategory.Drives, managementObject, "VolumeName");
            result.TryAdd <string>(SystemInfoCategory.Drives, managementObject, "VolumeSerialNumber");

            if (managementObject.TryGetProperty("Size", out ulong size) && managementObject.TryGetProperty("FreeSpace", out ulong freeSpace))
            {
                result.Add(new SystemInfoDto {
                    Name = "@Drives.Capacity", Value = new ProgressValueDto(size - freeSpace, size)
                });
            }

            return(result);
        }
Exemple #2
0
        public IEnumerable <KeyValuePair <string, object> > ProvideValues(ManagementObject managementObject, Process process, bool updateProcess)
        {
            if (!updateProcess)
            {
                //these values cannot change
                if (process != null)
                {
                    yield return(new KeyValuePair <string, object>("Name", process.ProcessName));
                }

                if (managementObject.TryGetDateTime("CreationDate", out var creationDate))
                {
                    yield return(new KeyValuePair <string, object>("CreationDate", creationDate));
                }

                if (managementObject.TryGetProperty("ExecutablePath", out string executablePath))
                {
                    yield return(new KeyValuePair <string, object>("ExecutablePath", executablePath));
                }

                if (managementObject.TryGetProperty("CommandLine", out string commandLine))
                {
                    yield return(new KeyValuePair <string, object>("CommandLine", commandLine));
                }
            }

            if (managementObject.TryGetProperty("ParentProcessId", out uint parentProcessId))
            {
                yield return(new KeyValuePair <string, object>("ParentProcessId", parentProcessId));
            }
        }
Exemple #3
0
 public IEnumerable <KeyValuePair <string, object> > ProvideValues(ManagementObject managementObject, Process process, bool updateProcess)
 {
     if (managementObject.TryGetProperty("ExecutablePath", out string executablePath))
     {
         yield return(new KeyValuePair <string, object>("Icon", FileUtilities.GetFileIcon(executablePath, 16)));
     }
 }
        public static T TryGetProperty <T>(this ManagementObject managementObject, string propertyName)
        {
            if (managementObject.TryGetProperty <T>(propertyName, out var value))
            {
                return(value);
            }

            return(default);
Exemple #5
0
 public static void TryAdd <T>(this IList <SystemInfoDto> dtos, string category, ManagementObject managementObject, string name,
                               Func <T, ValueDto> getValue)
 {
     if (managementObject.TryGetProperty(name, out T value))
     {
         var dto = new SystemInfoDto {
             Name = $"@{category}.{name}", Category = category, Value = getValue(value)
         };
         dtos.Add(dto);
     }
 }
        public IEnumerable <KeyValuePair <string, object> > ProvideValues(ManagementObject managementObject, Process process, bool updateProcess)
        {
            var result = new List <KeyValuePair <string, object> >();

            try
            {
                var handle = process.Handle; //possible exception

                string sid = null;
                try
                {
                    sid = ProcessExtensions.GetProcessOwner(process.Handle);
                    var processOwner = new SecurityIdentifier(sid).Translate(typeof(NTAccount)).ToString();
                    result.Add(new KeyValuePair <string, object>("ProcessOwner", processOwner));
                }
                catch (Exception)
                {
                    // ignored
                }

                if (IsImmersiveProcess(handle))
                {
                    result.Add(new KeyValuePair <string, object>("Status", ProcessType.Immersive));
                    return(result);
                }

                if (managementObject.TryGetProperty("SessionId", out uint sessionId) && sessionId == 0)
                {
                    result.Add(new KeyValuePair <string, object>("Status", ProcessType.Service));
                    return(result);
                }

                if (IsNetAssembly(managementObject))
                {
                    result.Add(new KeyValuePair <string, object>("Status", ProcessType.NetAssembly));
                    return(result);
                }

                if (sid != null && string.Equals(User.UserIdentity?.User?.Value, sid, StringComparison.OrdinalIgnoreCase))
                {
                    result.Add(new KeyValuePair <string, object>("Status", ProcessType.UserProcess));
                }
            }
            catch (Exception)
            {
                // no access to handle
            }

            return(result);
        }
        private bool IsNetAssembly(ManagementObject managementObject)
        {
            if (managementObject.TryGetProperty("ExecutablePath", out string executablePath))
            {
                try
                {
                    AssemblyName.GetAssemblyName(executablePath); //possible exception
                    return(true);
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            return(false);
        }
Exemple #8
0
        public static void TryAdd <T>(this IList <SystemInfoDto> dtos, string category, ManagementObject managementObject, string name)
        {
            if (managementObject.TryGetProperty(name, out T value))
            {
                var dto = new SystemInfoDto {
                    Name = $"@{category}.{name}", Category = category
                };

                switch (value)
                {
                case var val when val is string stringValue:
                    dto.Value = new TextValueDto(stringValue);
                    break;

                case var val when val is ulong ulongValue:
                    dto.Value = new NumberValueDto((long)ulongValue);
                    break;

                case var val when val is uint uintValue:
                    dto.Value = new NumberValueDto(uintValue);
                    break;

                case var val when val is bool boolValue:
                    dto.Value = new BoolValueDto(boolValue);
                    break;

                case var val when val is short number:
                    dto.Value = new NumberValueDto(number);
                    break;

                case var val when val is byte number:
                    dto.Value = new NumberValueDto(number);
                    break;

                case var val when val is ushort number:
                    dto.Value = new NumberValueDto(number);
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                dtos.Add(dto);
            }
        }
        public IEnumerable <KeyValuePair <string, object> > ProvideValues(ManagementObject managementObject, Process process, bool updateProcess)
        {
            if (!updateProcess)
            {
                if (managementObject.TryGetProperty("ExecutablePath", out string executablePath))
                {
                    var fileVersionInfo = FileVersionInfo.GetVersionInfo(executablePath);

                    yield return(new KeyValuePair <string, object>("Description", fileVersionInfo.FileDescription));

                    yield return(new KeyValuePair <string, object>("CompanyName", fileVersionInfo.CompanyName));

                    yield return(new KeyValuePair <string, object>("ProductVersion", fileVersionInfo.ProductVersion));

                    yield return(new KeyValuePair <string, object>("FileVersion", fileVersionInfo.FileVersion));
                }
            }
        }
Exemple #10
0
        private Task <ProcessDto> CreateProcessDto(ManagementObject managementObject, CancellationToken cancellationToken)
        {
            Process process;

            if (managementObject.TryGetProperty("ProcessId", out uint processId))
            {
                try
                {
                    process = Process.GetProcessById((int)processId);
                }
                catch (Exception)
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }

            var processDto = new ProcessDto {
                ProcessId = (int)processId
            };
            var isUpdate = _latestProcessIds?.Contains((int)processId) == true;

            foreach (var processValueProvider in _processValueProviders)
            {
                try
                {
                    foreach (var property in processValueProvider.ProvideValues(managementObject, process, isUpdate))
                    {
                        processDto.Add(property.Key, property.Value);
                    }
                }
                catch (Exception)
                {
                    // ignored
                }
            }

            return(Task.FromResult(processDto));
        }
Exemple #11
0
        //https://msdn.microsoft.com/en-us/library/windows/desktop/aa394372%28v=vs.85%29.aspx
        private static ProcessInfo ManagementObjectToProcessInfo(ManagementObject queryObj, Process process)
        {
            var result = new ProcessInfo
            {
                Name          = queryObj.TryGetProperty <string>("Name"),
                PrivateBytes  = process?.PrivateMemorySize64 ?? 0,
                WorkingSet    = (long)queryObj.TryGetProperty <ulong>("WorkingSetSize"),
                Id            = (int)queryObj.TryGetProperty <uint>("ProcessId"),
                ParentProcess = (int)queryObj.TryGetProperty <uint>("ParentProcessId"),
                StartTime     =
                    (ManagementExtensions.ToDateTimeSafe(queryObj.TryGetProperty <string>("CreationDate")) ??
                     ExceptionUtilities.EatExceptions(() => process?.StartTime) ?? DateTime.MinValue)
                    .ToUniversalTime(),
                Filename         = queryObj.TryGetProperty <string>("ExecutablePath"),
                CommandLine      = queryObj.TryGetProperty <string>("CommandLine"),
                MainWindowHandle = (long)(process?.MainWindowHandle ?? IntPtr.Zero)
            };

            ApplyProcessInformation(process, result, queryObj.TryGetProperty <string>("ExecutablePath"));

            return(result);
        }
        public static void ApplyProperties(this ChangingProcessPropertiesDto dto, Process process, ManagementObject wmiProcess)
        {
            if (process.HasExited)
            {
                dto.Status = ProcessStatus.Exited;
                return;
            }

            try
            {
                dto.PrivateBytes   = process.PrivateMemorySize64;
                dto.WorkingSetSize = process.WorkingSet64;
            }
            catch (Exception)
            {
                // ignored
            }

            dto.HandleCount        = process.HandleCount;
            dto.TotalProcessorTime = process.TotalProcessorTime;
            dto.UserProcessorTime  = process.UserProcessorTime;
            dto.Status             = process.Responding ? ProcessStatus.Running : ProcessStatus.NotResponding;

            if (wmiProcess.TryGetProperty("KernelModeTime", out ulong kernelModeTime))
            {
                dto.KernelModeTime = TimeSpan.FromTicks((long)kernelModeTime);
            }

            if (wmiProcess.TryGetProperty("UserModeTime", out ulong userModeTime))
            {
                dto.UserModeTime = TimeSpan.FromTicks((long)userModeTime);
            }

            if (wmiProcess.TryGetProperty("Priority", out uint priority))
            {
                dto.Priority = priority;
            }

            if (wmiProcess.TryGetProperty("PageFaults", out uint pageFaults))
            {
                dto.PageFaults = pageFaults;
            }

            if (wmiProcess.TryGetProperty("OtherOperationCount", out ulong otherOperationCount))
            {
                dto.OtherOperationCount = otherOperationCount;
            }

            if (wmiProcess.TryGetProperty("OtherTransferCount", out ulong otherTransferCount))
            {
                dto.OtherTransferCount = otherTransferCount;
            }

            if (wmiProcess.TryGetProperty("PeakPageFileUsage", out uint peakPageFileUsage))
            {
                dto.PeakPageFileUsage = peakPageFileUsage;
            }

            if (wmiProcess.TryGetProperty("PeakVirtualSize", out ulong peakVirtualSize))
            {
                dto.PeakVirtualSize = peakVirtualSize;
            }

            if (wmiProcess.TryGetProperty("PeakWorkingSetSize", out ulong peakWorkingSetSize))
            {
                dto.PeakWorkingSetSize = peakWorkingSetSize;
            }

            if (wmiProcess.TryGetProperty("PrivatePageCount", out ulong privatePageCount))
            {
                dto.PrivatePageCount = privatePageCount;
            }

            if (wmiProcess.TryGetProperty("PageFileUsage", out uint pageFileUsage))
            {
                dto.PageFileUsage = pageFileUsage;
            }

            if (wmiProcess.TryGetProperty("QuotaNonPagedPoolUsage", out uint quotaNonPagedPoolUsage))
            {
                dto.QuotaNonPagedPoolUsage = quotaNonPagedPoolUsage;
            }

            if (wmiProcess.TryGetProperty("QuotaPagedPoolUsage", out uint quotaPagedPoolUsage))
            {
                dto.QuotaPagedPoolUsage = quotaPagedPoolUsage;
            }

            if (wmiProcess.TryGetProperty("QuotaPeakNonPagedPoolUsage", out uint quotaPeakNonPagedPoolUsage))
            {
                dto.QuotaPeakNonPagedPoolUsage = quotaPeakNonPagedPoolUsage;
            }

            if (wmiProcess.TryGetProperty("QuotaPeakPagedPoolUsage", out uint quotaPeakPagedPoolUsage))
            {
                dto.QuotaPeakPagedPoolUsage = quotaPeakPagedPoolUsage;
            }

            if (wmiProcess.TryGetProperty("ReadOperationCount", out ulong readOperationCount))
            {
                dto.ReadOperationCount = readOperationCount;
            }

            if (wmiProcess.TryGetProperty("ReadTransferCount", out ulong readTransferCount))
            {
                dto.ReadTransferCount = readTransferCount;
            }

            if (wmiProcess.TryGetProperty("ThreadCount", out uint threadCount))
            {
                dto.ThreadCount = threadCount;
            }

            if (wmiProcess.TryGetProperty("VirtualSize", out ulong virtualSize))
            {
                dto.VirtualSize = virtualSize;
            }

            if (wmiProcess.TryGetProperty("WriteOperationCount", out ulong writeOperationCount))
            {
                dto.WriteOperationCount = writeOperationCount;
            }

            if (wmiProcess.TryGetProperty("WriteTransferCount", out ulong writeTransferCount))
            {
                dto.WriteTransferCount = writeTransferCount;
            }
        }