locked
How to get volume Serial Number using CLR function RRS feed

  • Question

  • Hi Experts,

    I am trying to create CLR function to get volume serial number from CLR function as like result comes if you hit command on command prompt as below:

    C:\vol C:

    Result :

     Volume in drive C is Windows
     Volume Serial Number is XXXX-XXXX

    using System;
    using System.Management;
    using System.Collections;
    
    namespace Test_SerialNum_Console
    {
        class Program
        {
            public static ArrayList hdResult;
    
            static void Main(string[] args)
            {
               // WMI m = new WMI();
                //hdResult = new ArrayList();
                Console.WriteLine("Enter Server Name=");
                string Input = Console.ReadLine();
                GetDisk(Input);
                Console.WriteLine("The Volume Serial Number is " + GetHDDSerial().ToString());
                Console.ReadLine();
                
            }
    
            public static void GetDisk(string computerName)
            {
                try
                {
                    ManagementScope scope = new ManagementScope(@"\\" + computerName + @"\root\cimv2");
                    scope.Connect();
                    ArrayList hdCollection = new ArrayList();
                    ManagementObjectSearcher searcher = new ManagementObjectSearcher();
    
                    ObjectQuery query1 = new ObjectQuery("SELECT * FROM Win32_volume where DriveLetter='C:'");
    
                    searcher = new ManagementObjectSearcher(scope, query1);
    
                    foreach (ManagementObject wmi_HD in searcher.Get())
                    {
                        try
                        {
                            if (wmi_HD["SerialNumber"].ToString() != "")
                            {
                                Console.WriteLine(wmi_HD["SerialNumber"].ToString());
                            }
    
                        }
                        catch(Exception e)
                        {
                            Console.WriteLine(e.Message);
                        }
    
                    }
    
                }
                catch (ManagementException)
                {
                    Console.WriteLine("No Output!!");
                }
            }

    I have created console application before creating CLR function for testing purpose, please suggest what should be the correct win32 class which will give me correct volume serial number which I have mentioned above.

    Regards,



    Niraj Sevalkar


    • Edited by Niraj Sevalkar Wednesday, December 5, 2018 1:31 PM Correcting heading
    Wednesday, December 5, 2018 1:30 PM

Answers

  • Hi All,

     I got the answer for this, I was throwing wrong query with wrong property, corrected query like below with correct property.

    ObjectQuery query1 = new ObjectQuery("SELECT * FROM Win32_LogicalDisk where name='C:'");
    
    output = wmi_HD["VolumeSerialNumber"].ToString();
    
    


    Regards,



    Niraj Sevalkar

    • Marked as answer by nielsbMVP Thursday, December 6, 2018 2:24 AM
    Wednesday, December 5, 2018 4:53 PM