2011-01-20 39 views
14

Có cách nào để lấy thông tin sau bằng cách sử dụng tập lệnh C#.Nhận thông tin máy tính (hệ thống) trên cửa sổ máy - C# script

PC Tên Service Tag CPU loại CPU tốc độ kích thước của c: \ ổ đĩa, cài đặt RAM, tên hệ điều hành, OS Product Key, Version Office, và Office Product Key.

Cảm ơn.

+10

BTW, C# thường không được gọi là 'script' –

+2

Evan - Tôi chưa thử bất cứ điều gì chưa – dps123

+0

@MitchWheat C# Script là một điều riêng biệt từ C#. Phần mở rộng tập tin '.csx', có thể chạy được bằng' csi.exe' kèm theo các phiên bản Visual Studio gần đây. –

Trả lời

10
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Management; //This namespace is used to work with WMI classes. For using this namespace add reference of System.Management.dll . 
using Microsoft.Win32;  //This namespace is used to work with Registry editor. 

namespace OperatingSystemInfo1 
{ 
    class TestProgram 
    { 
     static void Main(string[] args) 
     { 
      SystemInfo si = new SystemInfo();  //Create an object of SystemInfo class. 
      si.getOperatingSystemInfo();   //Call get operating system info method which will display operating system information. 
      si.getProcessorInfo();     //Call get processor info method which will display processor info. 
      Console.ReadLine();      //Wait for user to accept input key. 
     } 
    } 
    public class SystemInfo 
    { 
     public void getOperatingSystemInfo() 
     { 
      Console.WriteLine("Displaying operating system info....\n"); 
      //Create an object of ManagementObjectSearcher class and pass query as parameter. 
      ManagementObjectSearcher mos = new ManagementObjectSearcher("select * from Win32_OperatingSystem"); 
      foreach (ManagementObject managementObject in mos.Get()) 
      { 
       if (managementObject["Caption"] != null) 
       { 
        Console.WriteLine("Operating System Name : " + managementObject["Caption"].ToString()); //Display operating system caption 
       } 
       if (managementObject["OSArchitecture"] != null) 
       { 
        Console.WriteLine("Operating System Architecture : " + managementObject["OSArchitecture"].ToString()); //Display operating system architecture. 
       } 
       if (managementObject["CSDVersion"] != null) 
       { 
        Console.WriteLine("Operating System Service Pack : " + managementObject["CSDVersion"].ToString());  //Display operating system version. 
       } 
      } 
     } 

     public void getProcessorInfo() 
     { 
      Console.WriteLine("\n\nDisplaying Processor Name...."); 
      RegistryKey processor_name = Registry.LocalMachine.OpenSubKey(@"Hardware\Description\System\CentralProcessor\0", RegistryKeyPermissionCheck.ReadSubTree); //This registry entry contains entry for processor info. 

      if (processor_name != null) 
      { 
       if (processor_name.GetValue("ProcessorNameString") != null) 
       { 
        Console.WriteLine(processor_name.GetValue("ProcessorNameString")); //Display processor ingo. 
       } 
      } 
     } 
    } 
} 
1

Có một gói nuget gọi là MissingLinq.Linq2Management đã bao bọc khá nhiều mọi thứ về WMI thành một đối tượng được đánh máy mạnh mẽ. Có vẻ khá đẹp.

https://missinglinq.codeplex.com/

0

Để thực hiện việc này Thêm Microsoft.VB trong phòng làm việc của bạn. Để thêm quyền này, nhấp chuột phải vào thư mục Refrence của giải pháp khám phá và nhấp vào Thêm Refrences sau đó nhấp vào .NET và nhấp vào Microsoft.visualBasic sau đó nhấp vào ok. Sau khi thực hiện nhập khẩu này này như:

using Microsoft.VisualBasic.Devices; 

      public void getSystemDetails() 
      { 
       UserName.Text = Environment.UserName; // User name of PC 
       LabelOS.Text = getOSInfo(); // OS version of pc 
       MachineTxt.Text = Environment.MachineName;// Machine name 
       string OStype = ""; 
       if (Environment.Is64BitOperatingSystem) { OStype = "64-Bit, "; } else { OStype = "32-Bit, "; } 
       OStype += Environment.ProcessorCount.ToString() + " Processor"; 
       label8.Text = OStype; // Processor type 

       ulong toalRam = cinfo.TotalPhysicalMemory; 
       double toal = Convert.ToDouble(toalRam/(1024 * 1024)); 
       int t = Convert.ToInt32(Math.Ceiling(toal/1024).ToString()); 
       label6.Text = t.ToString() + " GB";// ram detail 
      } 

     public string getOSInfo() 
     { 
      //Get Operating system information. 
      OperatingSystem os = Environment.OSVersion; 
      //Get version information about the os. 
      Version vs = os.Version; 

      //Variable to hold our return value 
      string operatingSystem = ""; 

      if (os.Platform == PlatformID.Win32Windows) 
      { 
       //This is a pre-NT version of Windows 
       switch (vs.Minor) 
       { 
        case 0: 
         operatingSystem = "95"; 
         break; 
        case 10: 
         if (vs.Revision.ToString() == "2222A") 
          operatingSystem = "98SE"; 
         else 
          operatingSystem = "98"; 
         break; 
        case 90: 
         operatingSystem = "Me"; 
         break; 
        default: 
         break; 
       } 
      } 
      else if (os.Platform == PlatformID.Win32NT) 
      { 
       switch (vs.Major) 
       { 
        case 3: 
         operatingSystem = "NT 3.51"; 
         break; 
        case 4: 
         operatingSystem = "NT 4.0"; 
         break; 
        case 5: 
         if (vs.Minor == 0) 
          operatingSystem = "Windows 2000"; 
         else 
          operatingSystem = "Windows XP"; 
         break; 
        case 6: 
         if (vs.Minor == 0) 
          operatingSystem = "Windows Vista"; 
         else 
          operatingSystem = "Windows 7 or Above"; 
         break; 
        default: 
         break; 
       } 
0

Bạn có máy tính từ tên chính thức một giá trị mà bạn muốn, vì vậy bạn có thể có được điều này từ Environment.MachineName nếu bạn muốn máy tính cục bộ, hoặc bạn có thể làm IPHostEntry hostEntry = Dns.GetHostEntry(ip); sau đó string host = hostEntry.HostName; sử dụng DNS để giải quyết tên máy tính từ xa nếu bạn chỉ có IP của máy tính đó.

Bạn có thể nhận được một số thông tin từ registry, sau khi bạn kiểm tra xem Remote Registry đang chạy, giả sử bạn muốn có một máy tính từ xa:

ServiceController sc = new ServiceController("RemoteRegistry", computer); 
if (sc.Status.Equals(ServiceControllerStatus.Running)) 
{ 
    // do your stuff 
} 

Và bạn có thể bắt đầu nó nếu phát hiện ngừng:

if (sc.Status.Equals(ServiceControllerStatus.Stopped) || 
sc.Status.Equals(ServiceControllerStatus.StopPending)) 
{ 
    sc.Start(); 
} 

Thêm using bản tuyên Bố này để phía trên cùng của trang của bạn:

using Microsoft.Win32; 

Đối với tên máy tính, bạn có thể vào HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ ComputerName \ ActiveComputerName:

string path = @"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\ComputerName\ActiveComputerName"; 
RegistryKey rk = Registry.OpenRemoteBaseKey(RegistryHive.LocalMachine, computer).OpenSubKey(path); 
string pcName = rk.GetValue("computerName").ToString(); 

Đối với bất kỳ lệnh registry địa phương, chỉ cần loại bỏ Registry.OpenRemoteBaseKey(, computer).

Đối với tên CPU:

string path = @"HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\System\CentralProcessor\0"; 
RegistryKey rk = Registry.OpenRemoteBaseKey(RegistryHive.LocalMachine, computer).OpenSubKey(path); 
string cpuName = rk.GetValue("processorNameString").ToString(); 

Đối với Name OS và chìa khóa:

string path = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion"; 
RegistryKey rk = Registry.OpenRemoteBaseKey(RegistryHive.LocalMachine, computer).OpenSubKey(path); 
string osName = rk.GetValue("productName").ToString(); 
string servicePack = rk.GetValue("CSDVersion").ToString(); 
byte[] digitalProductId = registry.GetValue("DigitalProductId") as byte[]; 
string osProductKey = DecodeProductKey(digitalProductId); 

Từ Geeks With Blogs để nhận mã sản phẩm:

public static string DecodeProductKey(byte[] digitalProductId) 
{ 
    // Offset of first byte of encoded product key in 
    // 'DigitalProductIdxxx" REG_BINARY value. Offset = 34H. 
    const int keyStartIndex = 52; 
    // Offset of last byte of encoded product key in 
    // 'DigitalProductIdxxx" REG_BINARY value. Offset = 43H. 
    const int keyEndIndex = keyStartIndex + 15; 
    // Possible alpha-numeric characters in product key. 
    char[] digits = new char[] 
    { 
    'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'M', 'P', 'Q', 'R', 
    'T', 'V', 'W', 'X', 'Y', '2', '3', '4', '6', '7', '8', '9', 
    }; 
    // Length of decoded product key 
    const int decodeLength = 29; 
    // Length of decoded product key in byte-form. 
    // Each byte represents 2 chars. 
    const int decodeStringLength = 15; 
    // Array of containing the decoded product key. 
    char[] decodedChars = new char[decodeLength]; 
    // Extract byte 52 to 67 inclusive. 
    ArrayList hexPid = new ArrayList(); 
    for (int i = keyStartIndex; i <= keyEndIndex; i++) 
    { 
    hexPid.Add(digitalProductId[i]); 
    } 
    for (int i = decodeLength - 1; i >= 0; i--) 
    { 
    // Every sixth char is a separator. 
    if ((i + 1) % 6 == 0) 
    { 
     decodedChars[i] = '-'; 
    } 
    else 
    { 
     // Do the actual decoding. 
     int digitMapIndex = 0; 
     for (int j = decodeStringLength - 1; j >= 0; j--) 
     { 
     int byteValue = (digitMapIndex << 8) | (byte)hexPid[j]; 
     hexPid[j] = (byte)(byteValue/24); 
     digitMapIndex = byteValue % 24; 
     decodedChars[i] = digits[digitMapIndex]; 
     } 
    } 
    } 
    return new string(decodedChars); 
} 

Đó được những khó khăn hiện của con đường. Điểm là, đăng ký là bạn của bạn.

Các vấn đề liên quan