Hi MJ999,
This example is in VB.NET, but it can easily be translated into C#. In this example, I am applying the most recent snapshot of the virtual machine. To apply a specific snapshot, replace the 'lastSnapshot' with the Msvm_VirtualSystemSettingData instance of the snapshot you wish to apply.
Hopefully this will get you going, but please let me know if you still have any issues.
Imports System.Management
Public Class VirtualSystemSnapshot
Public Shared Function Apply(vm As ManagementObject, snapName As String)
Dim Options As New ConnectionOptions
Options.Username = strUserName
Options.Password = strPassword
' Now we can apply the snapshot
Dim scope As New ManagementScope("\\" & ServerName & "\Root\Virtualization\V2", Options)
Using virtualMachine As ManagementObject = WmiUtilities.GetVirtualMachine(vmElementName, scope)
Using virtualSystemSettingData As ManagementObject = WmiUtilities.GetVirtualSystemSettingData(scope, virtualMachine)
Using virtualSystemSnapshotService As ManagementObject = WmiUtilities.GetVirtualSystemSnapshotService(scope)
Using lastSnapshot As ManagementObject = WmiUtilities.GetFirstObjectFromCollection(
virtualSystemSettingData.GetRelated("Msvm_VirtualSystemSettingData", "Msvm_ParentChildSettingData", Nothing, Nothing, Nothing, Nothing, False, Nothing))
Using inParams As ManagementBaseObject = virtualSystemSnapshotService.GetMethodParameters("ApplySnapshot")
inParams("Snapshot") = lastSnapshot
' In order to apply a snapshot, the virtual machine must first be saved
RequestStateChange.Main(vm, RequestedState.Save)
Using outParams As ManagementBaseObject = virtualSystemSnapshotService.InvokeMethod("ApplySnapshot", inParams, Nothing)
WmiUtilities.ValidateOutput(outParams, scope)
' Now that the snapshot has been applied, start the VM back up
RequestStateChange.Main(vm, RequestedState.Start)
End Using
End Using
End Using
End Using
End Using
End Using
End Function
End Class