public static DiskGroupLockResult ExtendPartition(Partition volume, long numberOfAdditionalExtentSectors)
        {
            if (volume.Disk is PhysicalDisk)
            {
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    bool isReadOnly;
                    bool isOnline = ((PhysicalDisk)volume.Disk).GetOnlineStatus(out isReadOnly);
                    if (!isOnline || isReadOnly)
                    {
                        return(DiskGroupLockResult.OneOrMoreDisksAreOfflineOrReadonly);
                    }
                }

                LockStatus status = LockHelper.LockBasicDiskAndVolumesOrNone(((PhysicalDisk)volume.Disk));
                if (status == LockStatus.CannotLockDisk)
                {
                    return(DiskGroupLockResult.CannotLockDisk);
                }
                else if (status == LockStatus.CannotLockVolume)
                {
                    return(DiskGroupLockResult.CannotLockVolume);
                }

                if (Environment.OSVersion.Version.Major >= 6)
                {
                    bool success = ((PhysicalDisk)volume.Disk).SetOnlineStatus(false);
                    if (!success)
                    {
                        return(DiskGroupLockResult.CannotTakeDiskOffline);
                    }
                }
            }

            ExtendHelper.ExtendPartition(volume, numberOfAdditionalExtentSectors);

            if (volume.Disk is PhysicalDisk)
            {
                if (Environment.OSVersion.Version.Major >= 6)
                {
                    bool success = ((PhysicalDisk)volume.Disk).SetOnlineStatus(true);
                }
                LockHelper.UnlockBasicDiskAndVolumes((PhysicalDisk)volume.Disk);
                ((PhysicalDisk)volume.Disk).UpdateProperties();
            }

            return(DiskGroupLockResult.Success);
        }
        private static ExtendFileSystemResult ExtendUnmountedFileSystem(Partition volume, long numberOfAdditionalSectors)
        {
            IExtendableFileSystem fileSystem = FileSystemHelper.ReadFileSystem(volume) as IExtendableFileSystem;

            // Windows Vista / 7 enforce various limitations on direct write operations to volumes and disks.
            // Basic disks are not needed to be taken offline for direct write operations within volume region. Only dynamic disks have to.
            if (volume.Disk is PhysicalDisk && Environment.OSVersion.Version.Major >= 6)
            {
                bool isReadOnly;
                bool isOnline = ((PhysicalDisk)volume.Disk).GetOnlineStatus(out isReadOnly);
                if (!isOnline || isReadOnly)
                {
                    return(ExtendFileSystemResult.OneOrMoreDisksAreOfflineOrReadonly);
                }

                LockStatus status = LockHelper.LockBasicDiskAndVolumesOrNone(((PhysicalDisk)volume.Disk));
                if (status == LockStatus.CannotLockDisk)
                {
                    return(ExtendFileSystemResult.CannotLockDisk);
                }
                else if (status == LockStatus.CannotLockVolume)
                {
                    return(ExtendFileSystemResult.CannotLockVolume);
                }

                bool success = ((PhysicalDisk)volume.Disk).SetOnlineStatus(false);
                if (!success)
                {
                    return(ExtendFileSystemResult.CannotTakeDiskOffline);
                }
            }

            fileSystem.Extend(numberOfAdditionalSectors);

            if (volume.Disk is PhysicalDisk && (Environment.OSVersion.Version.Major >= 6))
            {
                bool success = ((PhysicalDisk)volume.Disk).SetOnlineStatus(true);
                LockHelper.UnlockBasicDiskAndVolumes((PhysicalDisk)volume.Disk);
            }

            return(ExtendFileSystemResult.Success);
        }
        public static void ReleaseDisk(Disk disk)
        {
            if (disk is DiskImage)
            {
                ((DiskImage)disk).ReleaseLock();
            }
            else if (disk is RAMDisk)
            {
                ((RAMDisk)disk).Free();
            }
#if Win32
            else if (disk is PhysicalDisk)
            {
                if (!DiskAccessLibrary.LogicalDiskManager.DynamicDisk.IsDynamicDisk(disk))
                {
                    LockHelper.UnlockBasicDiskAndVolumes((PhysicalDisk)disk);
                    try
                    {
                        ((PhysicalDisk)disk).UpdateProperties();
                    }
                    catch (System.IO.IOException)
                    {
                    }
                }
            }
            else if (disk is VolumeDisk)
            {
                bool skippedLock = (Environment.OSVersion.Version.Major >= 6 && VolumeInfo.IsOffline(((VolumeDisk)disk).Volume));
                if (!skippedLock)
                {
                    Guid?windowsVolumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(((VolumeDisk)disk).Volume);
                    if (windowsVolumeGuid.HasValue)
                    {
                        WindowsVolumeManager.ReleaseLock(windowsVolumeGuid.Value);
                    }
                }
            }
#endif
        }