2016-02-29 138 views
0

我們需要將我們的應用程序升級到通用Windows應用程序(UWP)框架。該應用程序的主要目的是收集診斷統計信息並通過其餘呼叫將它們發送回我們的服務器。如何從UWP應用程序獲取診斷統計信息

但是我沒有發現任何的API可用於獲取統計

此前我們

PerformanceCounter _memoryCounter = new PerformanceCounter(); 

public SystemProperty GetPhysicalMemory() 
    { 
     string s = _QueryComputerSystem("totalphysicalmemory"); 
     double totalphysicalmemory = Convert.ToDouble(s); 
     double d = _GetCounterValue(_memoryCounter, "Memory", "Available Bytes", null); 
     return new SystemProperty { PropertyName = "Physical Memory", Total = totalphysicalmemory, Used = totalphysicalmemory - d }; 
    } 
這回總使用和內存可用字節

。我們還收集了網絡和CPU統計信息。其中沒有一個與新框架兼容。我應該在哪些名稱空間中查看此功能?或者我需要使用Pinvoke(link)之類的東西?

回答

2

最後我發現調用本地函數是最好的解決方案。 鏈接:really good pinvoke site

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Runtime.InteropServices; 
using System.Text; 
using System.Threading.Tasks; 


namespace Monitoring 
{ 

    public static class NativeCallsWrapper 
    { 
     private static SYSTEM_INFO sysInfo = new SYSTEM_INFO(); 
     private static MEMORYSTATUSEX mem = new MEMORYSTATUSEX(); 

     [DllImport("kernel32.dll", SetLastError = false)] 
     public static extern void GetSystemInfo([In, Out] SYSTEM_INFO Info); 

     [return: MarshalAs(UnmanagedType.Bool)] 
     [DllImport("kernel32.dll", SetLastError = true)] 
     static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer); 

     static NativeCallsWrapper() 
     { 
      GetSystemInfo(sysInfo); 
      GlobalMemoryStatusEx(mem); 
     } 

     [StructLayout(LayoutKind.Explicit)] 
     public struct SYSTEM_INFO_UNION 

     { 

      [FieldOffset(0)] 
      public UInt32 OemId; 
      [FieldOffset(0)] 
      public UInt16 ProcessorArchitecture; 
      [FieldOffset(2)] 
      public UInt16 Reserved; 
     } 

     public struct SYSTEM_INFO 

     { 

      public SYSTEM_INFO_UNION CpuInfo; 
      public UInt32 PageSize; 
      public UInt32 MinimumApplicationAddress; 
      public UInt32 MaximumApplicationAddress; 
      public UInt32 ActiveProcessorMask; 
      public UInt32 NumberOfProcessors; 
      public UInt32 ProcessorType; 
      public UInt32 AllocationGranularity; 
      public UInt16 ProcessorLevel; 
      public UInt16 ProcessorRevision; 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     public class MEMORYSTATUSEX 
     { 
      public uint dwLength; 
      public uint dwMemoryLoad; 
      public ulong ullTotalPhys; 
      public ulong ullAvailPhys; 
      public ulong ullTotalPageFile; 
      public ulong ullAvailPageFile; 
      public ulong ullTotalVirtual; 
      public ulong ullAvailVirtual; 
      public ulong ullAvailExtendedVirtual; 
      public MEMORYSTATUSEX() 
      { 
       this.dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX)); 
      } 
     } 

     public static GeneralStatistics getGeneralStatistics() 
     { 
      GeneralStatistics generalStatistics = new GeneralStatistics(); 
      generalStatistics.numberOfProcesses = (int)sysInfo.NumberOfProcessors; 
      generalStatistics.memoryTotal = mem.ullTotalPhys/1048; 
      generalStatistics.memoryInUse = (mem.ullTotalPhys - mem.ullAvailPhys)/1048; 
      return generalStatistics; 
     } 
    } 
} 
1

可以使用的MemoryManager來獲得設備的內存

https://msdn.microsoft.com/en-us/library/windows.system.memorymanager.aspx

也可以得到設備

https://msdn.microsoft.com/en-us/library/windows/apps/windows.system.profile.aspx

更新的信息:下面是一個例子越來越多當前設備的詳細信息 https://www.suchan.cz/2015/08/uwp-quick-tip-getting-device-os-and-app-info/

+0

的MemoryManager是針對特定應用的統計信息,而不是系統作爲一個整體。我正在繼續使用Windows.System.Profile,但我沒有得到任何有用的硬件信息系統 –

+0

請檢查我更新了我的答案 – RicardoPons

+1

的示例謝謝您的更新,但該示例仍然是應用程序和操作系統信息。我需要整個系統的使用統計信息[網絡,CPU和內存]。 –

相關問題