Too many friends ask me how to get serial number of HardDisk by C# code. They are use many method include: C# WMI, Win32_LogicalDisk, ... . The keywords that they finding on Internet.
As using keywords to find how to get serial number of HardDisk of internet, they will use a big function, a complex function.
At this Tips, I will show you the easy way to get HardDisk ID by using cmd command.
Would you know cmd command is a very useful application to access on system. When you know the cmd command, you may almost access anything of your window system include software and hardware on your computer or any tablet, laptop... The look sound great, Is it? :D
Oke below function which I using C# function call cmd window console and use vol command to get serial number of HardDisk
public static string ExecuteCommandSync(object command) { try { // create the ProcessStartInfo using "cmd" as the program to be run, // and "/c " as the parameters. // Incidentally, /c tells cmd that we want it to execute the command that follows, // and then exit. System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command); // The following commands are needed to redirect the standard output. // This means that it will be redirected to the Process.StandardOutput StreamReader. procStartInfo.RedirectStandardOutput = true; procStartInfo.UseShellExecute = false; // Do not create the black window. procStartInfo.CreateNoWindow = true; // Now we create a process, assign its ProcessStartInfo and start it System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo = procStartInfo; proc.Start(); // Get the output into a string string result = proc.StandardOutput.ReadToEnd(); // Display the command output. return result; } catch (Exception) { // Log the exception return null; } }
Output:
After this post, would you like to use my function?
Any feedback or comment please leave your comment, we can discuss about it!
Zidane