private static ExtendFileSystemResult ExtendMountedFileSystem(Volume volume, Guid windowsVolumeGuid, long numberOfAdditionalSectors)
        {
            // Windows Vista / 7 enforce various limitations on direct write operations to volumes and disks.
            // We either have to take the disk(s) offline or use the OS volume handle for write operations.
            // http://msdn.microsoft.com/en-us/library/ff551353%28v=vs.85%29.aspx
            OperatingSystemVolume osVolume   = new OperatingSystemVolume(windowsVolumeGuid, volume.BytesPerSector, volume.Size);
            IExtendableFileSystem fileSystem = FileSystemHelper.ReadFileSystem(osVolume) as IExtendableFileSystem;

            if (fileSystem == null)
            {
                return(ExtendFileSystemResult.UnsupportedFileSystem);
            }

            bool success = WindowsVolumeManager.ExclusiveLock(windowsVolumeGuid, FileAccess.ReadWrite);

            if (!success)
            {
                return(ExtendFileSystemResult.CannotLockVolume);
            }

            // Dismounting the volume will make sure the OS have the correct filesystem size. (locking the volume is not enough)
            success = WindowsVolumeManager.DismountVolume(windowsVolumeGuid);
            if (!success)
            {
                return(ExtendFileSystemResult.CannotDismountVolume);
            }

            fileSystem.Extend(numberOfAdditionalSectors);

            WindowsVolumeManager.ReleaseLock(windowsVolumeGuid);
            return(ExtendFileSystemResult.Success);
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (listVolumes.SelectedItems.Count > 0)
            {
                Volume selectedVolume = (Volume)listVolumes.SelectedItems[0].Tag;
                if (selectedVolume is DynamicVolume && !((DynamicVolume)selectedVolume).IsOperational)
                {
                    MessageBox.Show("The selected volume is not operational", "Error");
                    return;
                }
                if (!chkReadOnly.Checked)
                {
                    bool skipLock = (Environment.OSVersion.Version.Major >= 6 && VolumeInfo.IsOffline(selectedVolume));
                    if (!skipLock)
                    {
                        Guid?volumeGuid = WindowsVolumeHelper.GetWindowsVolumeGuid(selectedVolume);
                        if (Environment.OSVersion.Version.Major >= 6)
                        {
                            // Windows Vista / 7 enforce various limitations on direct write operations to volumes and disks.
                            // We either have to take the disk(s) offline or use the OS volume handle for write operations.
                            if (!volumeGuid.HasValue)
                            {
                                MessageBox.Show("The selected volume is not recognized by your operating system");
                                return;
                            }
                            selectedVolume = new OperatingSystemVolume(volumeGuid.Value, selectedVolume.BytesPerSector, selectedVolume.Size, chkReadOnly.Checked);
                        }

                        bool isLocked = false;
                        if (volumeGuid.HasValue)
                        {
                            isLocked = WindowsVolumeManager.ExclusiveLock(volumeGuid.Value);
                        }
                        if (!isLocked)
                        {
                            MessageBox.Show("Unable to lock the volume", "Error");
                            return;
                        }
                    }
                }
                m_selectedVolume  = selectedVolume;
                m_isReadOnly      = chkReadOnly.Checked;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("No volume was selected", "Error");
                return;
            }
        }