嘗試在UWP應用程序中獲取一些WMI對象。在.net 4.6上運行VS2015。引用在'系統'中定義的「組件」聲明類型
我收到ForEach錯誤和方法調用,聲明「引用類型'組件'聲明它是在'系統'中定義的,錯誤爲CS7069。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
namespace SystemInfo
{
class wmiObject
{
static osDetails Program()
{
ManagementObjectCollection osDetailsCollection = getWMIObject("SELECT OSType, caption FROM Win32_OperatingSystem");
osDetails Details = new osDetails();
foreach (ManagementObject mo in osDetailsCollection)
{
Details.OSName = mo["Caption"].ToString();
}
osDetailsCollection = getWMIObject("SELECT Description, NumberOfLogicalProcessors, L3CacheSize from Win32_Processor");
foreach (ManagementObject mo in osDetailsCollection)
{
Details.NumberOfLogicalProcessors = mo["NumberOfLogicalProcessors"].ToString();
Details.L3CacheSize = mo["L3CacheSize"].ToString();
Details.Description = mo["Description"].ToString();
}
;
return Details;
}
static ManagementObjectCollection getWMIObject(string query)
{
ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(query);
ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
return osDetailsCollection;
}
class osDetails
{
public string Description;
public string OSName;
public string NumberOfLogicalProcessors;
public string L3CacheSize;
}
}
}
錯誤
Severity Code Description Project File Line
Error CS7069 Reference to type 'Component' claims it is defined in 'System', but it could not be found SystemInfo C:\Users\Luke\Documents\GitHub\lgDns\lgDns\SystemInfo\SystemInfo\osDetails.cs 41
Error CS7069 Reference to type 'Component' claims it is defined in 'System', but it could not be found SystemInfo C:\Users\Luke\Documents\GitHub\lgDns\lgDns\SystemInfo\SystemInfo\osDetails.cs 18
Error CS7069 Reference to type 'Component' claims it is defined in 'System', but it could not be found SystemInfo C:\Users\Luke\Documents\GitHub\lgDns\lgDns\SystemInfo\SystemInfo\osDetails.cs 20
Error CS7069 Reference to type 'Component' claims it is defined in 'System', but it could not be found SystemInfo C:\Users\Luke\Documents\GitHub\lgDns\lgDns\SystemInfo\SystemInfo\osDetails.cs 26
Error CS7069 Reference to type 'Component' claims it is defined in 'System', but it could not be found SystemInfo C:\Users\Luke\Documents\GitHub\lgDns\lgDns\SystemInfo\SystemInfo\osDetails.cs 28
Error CS7069 Reference to type 'Component' claims it is defined in 'System', but it could not be found SystemInfo C:\Users\Luke\Documents\GitHub\lgDns\lgDns\SystemInfo\SystemInfo\osDetails.cs 29
Error CS7069 Reference to type 'Component' claims it is defined in 'System', but it could not be found SystemInfo C:\Users\Luke\Documents\GitHub\lgDns\lgDns\SystemInfo\SystemInfo\osDetails.cs 30
任何幫助,將不勝感激。
我已經在C#控制檯應用程序中測試了相同的代碼,它看起來工作正常 - 沒有引用問題。我已經將System.Management引用添加到我的通用應用程序中......這可能是新通用應用程序構建的問題嗎? –
您不能在UWP應用程序中使用System.Management命名空間。請在此處查看適用於UWP的.net API https://msdn.microsoft.com/en-us/library/windows/apps/mt185501.aspx –
您是否修復了這個問題?我面臨同樣的問題 – Bug