Changeset 196


Ignore:
Timestamp:
3/10/2008 12:26:01 PM (6 years ago)
Author:
lowjoel
Message:

An implementation of GetDriveClusterSize? used for rounding up file sizes to the cluster size to remove cluster tips.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/Util/Volume.cs

    r129 r196  
    2323        [DllImport("Kernel32.dll")] 
    2424        public static extern DriveTypes GetDriveType(string rootPathName); 
     25 
     26        /// <summary> 
     27        /// Retrieves information about the specified disk, including the amount 
     28        /// of free space on the disk. 
     29        ///  
     30        /// The GetDiskFreeSpace function cannot report volume sizes that are 
     31        /// greater than 2 gigabytes (GB). To ensure that your application works 
     32        /// with large capacity hard drives, use the GetDiskFreeSpaceEx function. 
     33        /// </summary> 
     34        /// <param name="lpRootPathName">The root directory of the disk for which 
     35        /// information is to be returned. If this parameter is NULL, the function 
     36        /// uses the root of the current disk. If this parameter is a UNC name, 
     37        /// it must include a trailing backslash (for example, \\MyServer\MyShare\). 
     38        /// Furthermore, a drive specification must have a trailing backslash 
     39        /// (for example, C:\). The calling application must have FILE_LIST_DIRECTORY 
     40        /// access rights for this directory.</param> 
     41        /// <param name="lpSectorsPerCluster">A pointer to a variable that receives 
     42        /// the number of sectors per cluster.</param> 
     43        /// <param name="lpBytesPerSector">A pointer to a variable that receives 
     44        /// the number of bytes per sector.</param> 
     45        /// <param name="lpNumberOfFreeClusters">A pointer to a variable that 
     46        /// receives the total number of free clusters on the disk that are 
     47        /// available to the user who is associated with the calling thread. 
     48        ///  
     49        /// If per-user disk quotas are in use, this value may be less than the  
     50        /// total number of free clusters on the disk.</param> 
     51        /// <param name="lpTotalNumberOfClusters">A pointer to a variable that 
     52        /// receives the total number of clusters on the disk that are available 
     53        /// to the user who is associated with the calling thread. 
     54        ///  
     55        /// If per-user disk quotas are in use, this value may be less than the 
     56        /// total number of clusters on the disk.</param> 
     57        /// <returns>If the function succeeds, the return value is nonzero. 
     58        ///  
     59        /// If the function fails, the return value is zero. To get extended 
     60        /// error information, call GetLastError.</returns> 
     61        [DllImport("Kernel32.dll")] 
     62        internal static extern uint GetDiskFreeSpace( 
     63            [MarshalAs(UnmanagedType.LPStr)]string lpRootPathName, 
     64            out UInt32 lpSectorsPerCluster, out UInt32 lpBytesPerSector, 
     65            out UInt32 lpNumberOfFreeClusters, out UInt32 lpTotalNumberOfClusters); 
     66 
     67        /// <summary> 
     68        /// Determines the cluster size of the given volume. 
     69        /// </summary> 
     70        /// <param name="driveName">The path to the root of the drive, including 
     71        /// the trailing \</param> 
     72        /// <returns>The size of one cluster, in bytes.</returns> 
     73        public static uint GetDriveClusterSize(string driveName) 
     74        { 
     75            UInt32 clusterSize = 0, sectorSize, freeClusters, totalClusters; 
     76            if (GetDiskFreeSpace(driveName, out clusterSize, out sectorSize,  
     77                out freeClusters, out totalClusters) != 0) 
     78                return clusterSize * sectorSize; 
     79 
     80            throw new Exception("Unknown error calling GetDiskFreeSpace()"); 
     81        } 
    2582    } 
    2683 
Note: See TracChangeset for help on using the changeset viewer.