Ignore:
Timestamp:
6/18/2010 11:33:59 AM (2 years ago)
Author:
lowjoel
Message:

Allow us to query the size of non-ready partitions and disks (for more informative UIs)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/eraser/Eraser.Util/PhysicalDriveInfo.cs

    r2186 r2187  
    6565                for (int i = 0; ; ++i) 
    6666                { 
    67                     using (SafeFileHandle handle = OpenWin32Device(GetDiskPath(i))) 
     67                    using (SafeFileHandle handle = OpenWin32Device(GetDiskPath(i), 
     68                        NativeMethods.FILE_READ_ATTRIBUTES)) 
    6869                    { 
    6970                        if (handle.IsInvalid) 
     
    8283        /// </summary> 
    8384        /// <param name="deviceName">The name of the device to open.</param> 
     85        /// <param name="access">The access needed for the handle.</param> 
    8486        /// <returns>A <see cref="SafeFileHandle"/> to the device.</returns> 
    85         private static SafeFileHandle OpenWin32Device(string deviceName) 
     87        private static SafeFileHandle OpenWin32Device(string deviceName, uint access) 
    8688        { 
    8789            //Define the DOS device name for access 
     
    100102                //Open the device handle. 
    101103                return NativeMethods.CreateFile(string.Format(CultureInfo.InvariantCulture, 
    102                     "\\\\.\\{0}", dosDeviceName), NativeMethods.FILE_READ_ATTRIBUTES, 
     104                    "\\\\.\\{0}", dosDeviceName), access, 
    103105                    NativeMethods.FILE_SHARE_READ | NativeMethods.FILE_SHARE_WRITE, IntPtr.Zero, 
    104106                    (int)FileMode.Open, (uint)FileAttributes.ReadOnly, IntPtr.Zero); 
     
    131133                { 
    132134                    string path = GetPartitionPath(i); 
    133                     using (SafeFileHandle handle = OpenWin32Device(path)) 
     135                    using (SafeFileHandle handle = OpenWin32Device(path, 
     136                        NativeMethods.FILE_READ_ATTRIBUTES)) 
    134137                    { 
    135138                        if (handle.IsInvalid) 
     
    161164 
    162165        /// <summary> 
     166        /// Gets the size of the disk, in bytes. 
     167        /// </summary> 
     168        public long Size 
     169        { 
     170            get 
     171            { 
     172                using (SafeFileHandle handle = OpenWin32Device(GetDiskPath(), 
     173                    NativeMethods.GENERIC_READ)) 
     174                { 
     175                    if (handle.IsInvalid) 
     176                        throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 
     177 
     178                    long result = 0; 
     179                    uint returned = 0; 
     180                    if (NativeMethods.DeviceIoControl(handle, NativeMethods.IOCTL_DISK_GET_LENGTH_INFO, 
     181                        IntPtr.Zero, 0, out result, out returned, IntPtr.Zero)) 
     182                    { 
     183                        return result; 
     184                    } 
     185 
     186                    throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 
     187                } 
     188            } 
     189        } 
     190 
     191        /// <summary> 
    163192        /// The format string for accessing partitions. 
    164193        /// </summary> 
Note: See TracChangeset for help on using the changeset viewer.