none
How to mount a DVD iso programatically with C# RRS feed

  • Question

  • Can anybody add me any easy sample share about this topic?

    I try it with PowerShell succesfully on a Server 2019 with VS2019 and C# but I need sample for native WMI version.

    Or write me please any place where anybody can help me.
    Wednesday, April 1, 2020 8:47 AM

Answers

  • You can use the Shell to mount a .iso

    or MSFT_DiskImage with WMI

    For example with the Shell (test with hardcoded "mount" in English or French, should be localized) =>

    string sFilePath = @"E:\Test.iso";
    string sPath = Path.GetDirectoryName(sFilePath);
    string sFileName = Path.GetFileName(sFilePath);
    
    dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
    dynamic folder = shellApplication.NameSpace(sPath);
    dynamic folderItem = folder.ParseName(sFileName);
    dynamic folderItemVerbs = folderItem.Verbs();
    for (int i = 0; i < folderItemVerbs.Count(); i++)
    {
        dynamic folderItemVerb = folderItemVerbs.Item(i);
        if (folderItemVerb.Name.Equals("Mount") || folderItemVerb.Name.Equals("Monter"))
        {
            folderItemVerb.DoIt();
            break;
        }
    }


    • Edited by Castorix31 Wednesday, April 1, 2020 9:59 AM
    • Marked as answer by Zoltan Papp Hu Saturday, April 4, 2020 8:51 AM
    Wednesday, April 1, 2020 9:42 AM
  • BTW, the code above is not "Shell script" it is Shell method in C#...

    The WMI method =>

    string sISOFilePath = @"E:\Test.iso";
    // With help of WMICodeCreator
    try
    {    
        ManagementObject classInstance = new ManagementObject("root\\Microsoft\\Windows\\Storage",
            "MSFT_DiskImage.ImagePath='" + sISOFilePath + "',StorageType=1",
            null);
    
        // Obtain in-parameters for the method
        ManagementBaseObject inParams = classInstance.GetMethodParameters("Mount");
    
        // Add the input parameters.
        //inParams["NoDriveLetter"] = 3;
    
        // Execute the method and obtain the return values.
        ManagementBaseObject outParams = classInstance.InvokeMethod("Mount", inParams, null);
    
        // List outParams
        Console.WriteLine("Out parameters:");
        Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
    }
    catch (ManagementException err)
    {
        System.Windows.Forms.MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
    }

    Friday, April 3, 2020 2:52 PM

All replies

  • Can anybody add me any easy sample share about this topic?

    I try it with PowerShell succesfully on a Server 2019 with VS2019 and C# but I need sample for native WMI version.

    Or write me please any place where anybody can help me.
    Wednesday, April 1, 2020 8:46 AM
  • You can use the Shell to mount a .iso

    or MSFT_DiskImage with WMI

    For example with the Shell (test with hardcoded "mount" in English or French, should be localized) =>

    string sFilePath = @"E:\Test.iso";
    string sPath = Path.GetDirectoryName(sFilePath);
    string sFileName = Path.GetFileName(sFilePath);
    
    dynamic shellApplication = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));
    dynamic folder = shellApplication.NameSpace(sPath);
    dynamic folderItem = folder.ParseName(sFileName);
    dynamic folderItemVerbs = folderItem.Verbs();
    for (int i = 0; i < folderItemVerbs.Count(); i++)
    {
        dynamic folderItemVerb = folderItemVerbs.Item(i);
        if (folderItemVerb.Name.Equals("Mount") || folderItemVerb.Name.Equals("Monter"))
        {
            folderItemVerb.DoIt();
            break;
        }
    }


    • Edited by Castorix31 Wednesday, April 1, 2020 9:59 AM
    • Marked as answer by Zoltan Papp Hu Saturday, April 4, 2020 8:51 AM
    Wednesday, April 1, 2020 9:42 AM
  • Have a look at using the OpenVirtualDisk and AttachVirtualDisk APIs. I don't know if there's a managed equivalent so you may need to use PInvoke.
    Wednesday, April 1, 2020 11:34 AM
  • There is a virtual DVD iso management in WMI. Please use C# for examples.

    Wednesday, April 1, 2020 2:56 PM
  • There is a virtual DVD iso management in WMI, but I can't find out how can I use.
    Wednesday, April 1, 2020 3:00 PM
  • There is a virtual DVD iso image management in WMI.

    I don't understand what you mean

    With WMI, you just use Mount method of the MSFT_DiskImage class

    Wednesday, April 1, 2020 3:04 PM
  • There is a virtual DVD iso image management in WMI.

    I don't understand what you mean

    With WMI, you just use Mount method of the MSFT_DiskImage class

    Somewhere in Msvm_AllocationCapabilities there is a "ISO Disk Image Allocation Capabilities" and a method named "GetStorageAllocationSettingData". This and "Msvm_VirtualSystemManagementService.AddResourceSettings" work together somehow.
    I can't figure out more precisely because it is in a complexe software, and can't copy whole here.



    Friday, April 3, 2020 10:02 AM
  • I don't see the relation with your question

    "How to mount a DVD iso programatically with C#"

    The Shell code I posted mounts a DVD .iso or, better, the Mount method of the MSFT_DiskImage class

    (I tested both methods on Windows 10)
    • Edited by Castorix31 Friday, April 3, 2020 10:18 AM
    Friday, April 3, 2020 10:12 AM
  • I don't see the relation with your question

    "How to mount a DVD iso programatically with C#"

    The Shell code I posted mounts a DVD .iso or, better, the Mount method of the MSFT_DiskImage class

    (I tested both methods on Windows 10)

    Please copy your MSFT_DiskImage solution here.

    The program that I write above drop me Error Message: 'Hyper-V Server' failed to add device 'Virtual CD/DVD Disk'.

    I need C# solution, shell script is not good for this.

    Friday, April 3, 2020 2:09 PM
  • BTW, the code above is not "Shell script" it is Shell method in C#...

    The WMI method =>

    string sISOFilePath = @"E:\Test.iso";
    // With help of WMICodeCreator
    try
    {    
        ManagementObject classInstance = new ManagementObject("root\\Microsoft\\Windows\\Storage",
            "MSFT_DiskImage.ImagePath='" + sISOFilePath + "',StorageType=1",
            null);
    
        // Obtain in-parameters for the method
        ManagementBaseObject inParams = classInstance.GetMethodParameters("Mount");
    
        // Add the input parameters.
        //inParams["NoDriveLetter"] = 3;
    
        // Execute the method and obtain the return values.
        ManagementBaseObject outParams = classInstance.InvokeMethod("Mount", inParams, null);
    
        // List outParams
        Console.WriteLine("Out parameters:");
        Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
    }
    catch (ManagementException err)
    {
        System.Windows.Forms.MessageBox.Show("An error occurred while trying to execute the WMI method: " + err.Message);
    }

    Friday, April 3, 2020 2:52 PM
  • Sorry I forget that I need a Hyper-V VirtualMachine solution. Do you have it?

     ManagementScope scope = new ManagementScope(@"root\virtualization\v2");
                using (ManagementObject vm = WmiUtilities.GetVirtualMachine("teszt1", scope))
                using (ManagementObject vssd = WmiUtilities.GetVirtualMachineSettings(vm))
                using (ManagementObject vmms = WmiUtilities.GetVirtualMachineManagementService(scope))
                {
    ...
                }


    Saturday, April 4, 2020 8:29 AM