Search code examples
c#.netazurevhd

How do I mount a VHD using C# code without invoking a diskpart script?


In my Azure role I want to mount a VHD - roughly have an equivalent of the following diskpart commands:

vdisk select VhdFilePath
vdisk attach

This answer explains how to create a script and then programmatically invoke it from C#.

Is there a way to achieve the same without a script - just with some .NET classes?


Solution

  • I don't know if there's a managed API to do this, but there are a few examples of C# wrappers around some native methods:

    http://www.jmedved.com/2009/05/open-and-attach/

    And since this wraps around the AttachVirtualDisk method I assume it should also work for Windows Server 2008 R2 (note that you'll need to set the osFamily of your role to 2 in order to get the R2).