代码之家  ›  专栏  ›  技术社区  ›  Doug Kavendek

查询机器规格

  •  3
  • Doug Kavendek  · 技术社区  · 16 年前

    我尝试过各种搜索,但只能找到需要用户交互或必须安装的程序和GUI程序。

    或者,一个小的命令行程序也可以工作,只要我们被允许在测试应用程序中发布它。

    我找到了一个程序可以得到我想要的一些规格, PsInfo . 然而,它似乎要求每个用户在第一次运行时都同意某些许可证,即使它是一个命令行应用程序。另外,它只处理OS/CPU信息,我还需要更多。

    另外:忘了明确提到,但这确实只对Windows机器是必要的。你们真快!

    编辑:这个WMI看起来确实像我需要的,谢谢!不过有很多虫子,所以我得潜进去。它提到,对于某些事情,用户必须具有管理员权限;这可能不会是一个大问题,但它可能会限制它一点。

    6 回复  |  直到 16 年前
        1
  •  5
  •   Matt Hanson    16 年前

    也许会考虑使用 Windows Management Instrumentation (WMI),假设它是您计划查询的Windows计算机。看一看 WMI Code Creator

    WMI在Windows 2000时代一直运行良好,但在一些帮助下也可以在Win98机器上运行。

    只要您拥有或能够为您试图查询的机器提供管理员凭据,WMI肯定是一种选择。

        2
  •  4
  •   Rob Walker    16 年前

    对于此类信息 WMI 他是你的朋友。幸运的是,在.NET中处理WMI比在非托管世界中容易得多。有相当多的文章可以开始,比如 this one this one

        3
  •  1
  •   Doug Kavendek    16 年前

    必须在c#项目中包括对系统管理的参考。然后,源代码本身可能是非常糟糕的c#,但我以前从未真正写过它,它是一个内部工具,所以这有点离题。我已经简化了一点,所以它只处理输出机器规格(该工具还做其他事情)。每次调用LogClass()都会转储其所有属性。要转储更多类,只需查看上的MSDN部分 WMI Classes .

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Management;
    using System.IO;
    
    namespace SyTest
    {
      class Program
      {
        static StreamWriter specStream;
    
        static void Main(string[] args)
        {
          FileStream specFile =
              new FileStream("machine-specs.txt",FileMode.Create,FileAccess.Write);
          specStream = new StreamWriter(specFile);
    
          LogClass("Win32_DesktopMonitor");
          LogClass("Win32_VideoController");
          LogClass("Win32_Processor");
          // etc
    
          specStream.Close();
          specFile.Close();
        }
    
        static void LogClass(string strTable)
        {
          if (strTable.Length <= 0) return;
          specStream.Write("--- " + strTable + " ---\r\n\r\n");
          WqlObjectQuery wqlQuery =
              new WqlObjectQuery("SELECT * FROM " + strTable);
          ManagementObjectSearcher searcher =
              new ManagementObjectSearcher(wqlQuery);
          try
          {
            if (searcher.Get().Count <= 0)
            {
              specStream.Write("Class has no instances\r\n\r\n");
            }
            foreach (ManagementObject obj in searcher.Get())
            {
              specStream.Write("* " + obj.ToString() + "\r\n");
    
              if (obj.Properties.Count <= 0)
              {
                specStream.Write("Class instance has no properties\r\n");
                continue;
              }
    
              foreach (System.Management.PropertyData prop in obj.Properties)
              {
                LogAttr(obj, prop.Name);
              }
    
              specStream.Write("\r\n");
            }
          }
          catch { specStream.Write("Class does not exist\r\n\r\n"); }
        }
        static void LogAttr(ManagementObject obj, string str)
        {
          if (str.Length <= 0) return;
          string strValue = "";
          try
          {
            strValue = obj[str].ToString();
            try
            {
              string[] pstrTmp = ((string[])obj[str]);
              if (pstrTmp.Length > 0) strValue = String.Join(", ", pstrTmp);
            }
            catch { } // Problem casting, fall back on original assignment
          }
          catch { strValue = "[UNDEFINED]"; }
          specStream.Write(str + ": " + strValue + "\r\n");
        }
      }
    }
    
        4
  •  0
  •   Henrik Paul    16 年前

    对于为linux开发的奇怪情况,您可以在 /proc

        5
  •  0
  •   Millhouse    16 年前

    WMI Code Creator 来自微软。它使处理WMI变得非常简单。

        6
  •  0
  •   viggity    11 年前

    MissingLinq.Linq2Management 这将WMI的几乎所有内容包装到了一个漂亮的强类型对象中。看起来很不错。

    https://missinglinq.codeplex.com/