I needed to do it for a windows application I’m writing. My problem was to get the current serial number windows provides when a drive is formatted. You know, the one that appears when you type the “dir” command in a ms-dos prompt window. I googled around but I found only code for VB6 or code which should not have worked in a console application.
No default .NET object was giving me the piece of information I needed, so I though to get It directly from the “dir” command.
I got how to launch a process and get its output from EggHeadCafè but I added some work by me.
Calling directly the “dir” command didn’t work. But adding it as paramater to the “cmd” command gave no problem (complete command is “cmd /C dir”). This has been tested on an italian window xp professional. Let me know please if it has problems on other countries versions. According to the output of the “dir” command an English version of Windows 7 Ultimate, it should work.
Here is the code. Hope this can Help.
Function getHDSerial() As String Dim dir_cmd As New Process dir_cmd.StartInfo.UseShellExecute = False dir_cmd.StartInfo.RedirectStandardOutput = True dir_cmd.StartInfo.RedirectStandardError = True dir_cmd.StartInfo.CreateNoWindow = True dir_cmd.StartInfo.FileName = "cmd" dir_cmd.StartInfo.Arguments = "/C dir" dir_cmd.Start() Dim output As String dir_cmd.StandardOutput.ReadLine() ' Discard first line output = dir_cmd.StandardOutput.ReadLine ' second line should contain the serial number Dim splitted() As String splitted = output.Split(":") output = splitted(1).Trim() Return output End Function



