【问题标题】:Reference to type 'Component' claims it is defined in 'System'引用类型“组件”声称它在“系统”中定义
【发布时间】:2015-10-05 23:51:23
【问题描述】:

试图在 UWP 应用程序中获取一些 WMI 对象。在 .net 4.6 上运行 VS2015。

我收到 ForEach 和方法调用的错误,指出“对类型 'Component' 的引用声称它是在 'System' 中定义的”,错误为 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 msdn.microsoft.com/en-us/library/windows/apps/mt185501.aspx
  • 你修好了吗?我也面临同样的问题

标签: c# uwp wmi-query system.management


【解决方案1】:

我刚刚遇到这个问题,我找到的解决方案是将项目重新制作为C# WindowsFormsApp。它自动添加了引用:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\Microsoft.CSharp.dll
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.dll
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Core.dll
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Data.dll
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Data.DataSetExtensions.dll
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Deployment.dll
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Drawing.dll
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Net.Http.dll
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Windows.Forms.dll
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Xml.dll
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Xml.Linq.dll

希望这可以帮助某人。

【讨论】:

    【解决方案2】:

    这可能对你有用

    foreach (ManagementObject mo in osDetailsCollection)
    {
        foreach (PropertyData prop in mo.Properties)
        {
            if(prop.Name == "Caption")
            Details.OSName = prop.Value;
        }
    }
    

    【讨论】:

    • 不幸的是,它减少了我遇到的错误数量,但仍然得到相同的错误——对于每个外部 foreach 和 osObjDetails 上的 .Get() 方法。感谢您的关注:)
    猜你喜欢
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    • 2018-11-23
    • 2018-09-12
    • 2018-10-21
    相关资源
    最近更新 更多