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(
và , 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.
BTW, C# thường không được gọi là 'script' –
Evan - Tôi chưa thử bất cứ điều gì chưa – dps123
@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. –