Jak mogę uzyskać numer seryjny dysku twardego bez użycia dll i obsługiwane przez VISTAJak używać C#, aby uzyskać numer seryjny dysku twardego?
8
A
Odpowiedz
2
Spróbuj kod z here i daj nam znać, czy to działa:
// Namespace Reference
using System.Management;
/// <summary>
/// method to retrieve the selected HDD's serial number
/// </summary>
/// <param name="strDriveLetter">Drive letter to retrieve serial number for</param>
/// <returns>the HDD's serial number</returns>
public string GetHDDSerialNumber(string drive)
{
//check to see if the user provided a drive letter
//if not default it to "C"
if (drive == "" || drive == null)
{
drive = "C";
}
//create our ManagementObject, passing it the drive letter to the
//DevideID using WQL
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + drive +":\"");
//bind our management object
disk.Get();
//return the serial number
return disk["VolumeSerialNumber"].ToString();
}
EDIT: A jeśli to nie robi praca, spróbuj tego kodu z CodeProject:
Najpierw stwórzmy klasy do przechowywania informacji o dysku twardym:
class HardDrive
{
private string model = null;
private string type = null;
private string serialNo = null;
public string Model
{
get {return model;}
set {model = value;}
}
public string Type
{
get {return type;}
set {type = value;}
}
public string SerialNo
{
get {return serialNo;}
set {serialNo = value;}
}
}
Następnie, kwerendy Win32_DiskDrive Klasa:
ManagementObjectSearcher searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach(ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive();
hd.Model = wmi_HD["Model"].ToString();
hd.Type = wmi_HD["InterfaceType"].ToString();
hdCollection.Add(hd);
}
Teraz musimy wyodrębnić numer seryjny z klasy Win32_PhysicalMedia :
searcher = new
ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
int i = 0;
foreach(ManagementObject wmi_HD in searcher.Get())
{
// get the hard drive from collection
// using index
HardDrive hd = (HardDrive)hdCollection[i];
// get the hardware serial no.
if (wmi_HD["SerialNumber"] == null)
hd.SerialNo = "None";
else
hd.SerialNo = wmi_HD["SerialNumber"].ToString();
++i;
}
Teraz wyświetlania informacji naszego dysku twardego:
// Display available hard drives
foreach(HardDrive hd in hdCollection)
{
Console.WriteLine("Model\t\t: " + hd.Model);
Console.WriteLine("Type\t\t: " + hd.Type);
Console.WriteLine("Serial No.\t: " + hd.SerialNo);
Console.WriteLine();
}
14
using System.Management;
public string GetHDDSerial()
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
foreach (ManagementObject wmi_HD in searcher.Get())
{
// get the hardware serial no.
if (wmi_HD["SerialNumber"] != null)
return wmi_HD["SerialNumber"].ToString();
}
return string.Empty;
}
0
Cześć Właśnie znalazłem ten link
pracował dla mnie:
Herre jest istotną częścią kodu:
/// <summary>
/// return Volume Serial Number from hard drive
/// </summary>
/// <param name="strDriveLetter">[optional] Drive letter</param>
/// <returns>[string] VolumeSerialNumber</returns>
public string GetVolumeSerial(string strDriveLetter)
{
if(strDriveLetter=="" || strDriveLetter==null)
strDriveLetter="C";
ManagementObject disk = new ManagementObject("win32_logicaldisk.deviceid=\"" + strDriveLetter +":\"");
disk.Get();
return disk["VolumeSerialNumber"].ToString();
}
4
tutaj jest kod, który działa dla mnie:
ManagementObjectSearcher searcher = null;
searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive();
try
{
txtmdl.Text = hd.Caption = wmi_HD["Caption"].ToString();
txtsn.Text=(hd.SerialNo = wmi_HD.GetPropertyValue("SerialNumber").ToString());
Właśnie dodałem 2 tekstowym, aby uzyskać wyniki ,, można zrobić to samo!
i dysk twardy klasy:
public class HardDrive
{
public string Model { get; set; }
public string InterfaceType { get; set; }
public string Caption { get; set; }
public string SerialNo { get; set; }
}
Nie zapomnij użyć:
using System.Management;
Powiązane problemy
- 1. Uzyskaj prawdziwy numer seryjny dysku twardego z Delphi
- 2. Jak uzyskać numer seryjny pamięci USB w języku C#
- 3. Polecenie, aby zdalnie uzyskać specyfikację dysku twardego i procesora
- 4. tworzenie wirtualnego dysku twardego
- 5. Atmel UC3 - jak uzyskać numer seryjny?
- 6. Jak programowo uzyskać numer seryjny iPhone'a?
- 7. Jak uzyskać numer seryjny telefonu (IMEI)?
- 8. Jak ustawić numer seryjny NSOperationQueue?
- 9. Uzyskaj całkowity rozmiar dysku twardego w systemie C++ Windows
- 10. Mapowanie dysku sieciowego bez kodowania dysku twardego w pliku wsadowym
- 11. Jak odczytać numer seryjny z urządzenia mobilnego za pomocą C#?
- 12. Zapisz i załaduj PrivateKey crypto/rsa do iz dysku twardego
- 13. Najlepszy sposób, aby uzyskać aktualny numer miesiąca w C#
- 14. Jak zmienić rozmiar obrazu w języku C# na określony rozmiar dysku twardego?
- 15. Jak można wykryć główną literę dysku twardego, na przykład dysk C:?
- 16. Konwersja Excela Numer seryjny DateTime na R DateTime
- 17. Pisanie tablic bajtowych na numer seryjny z Ruby
- 18. Jak uzyskać numer seryjny procesora Raspberry PI 2 z systemem Windows IOT
- 19. Jak uzyskać numer wiersza błędu w programie C++
- 20. Objective-C regex aby sprawdzić numer telefonu
- 21. Jak otworzyć plik outlook .msg z mojego dysku twardego, który NIE jest w programie Outlook?
- 22. Jak uzyskać numer wersji bash
- 23. Jak uzyskać numer bieżącej linii?
- 24. Jak uzyskać numer porządkowy elementu
- 25. Jak uzyskać numer wersji JavaFX?
- 26. Jak uzyskać numer wersji F #?
- 27. Umbraco, jak używać identyfikatora właściwości obrazu, aby uzyskać adres URL
- 28. Boost asio socket: najszybszy sposób na odczytanie pliku z dysku twardego?
- 29. Jak używać FB.api ("/ me/picture"), aby uzyskać obraz profilu
- 30. jak używać process.hrtime, aby uzyskać czas wykonania funkcji asynchronicznej
Tak, zgadzam się ... WMI. –
Dobry pomysł dla mnie –