Ignore:
Timestamp:
5/16/2010 11:45:22 AM (2 years ago)
Author:
lowjoel
Message:

Reorganise the extension methods by the class it extends.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/eraser/Eraser.Util/ExtensionMethods/IO.cs

    r2148 r2151  
    5050 
    5151        /// <summary> 
     52        /// Gets the parent directory of the current <see cref="System.IO.FileSystemInfo"/> 
     53        /// object. 
     54        /// </summary> 
     55        /// <param name="info">The <see cref="System.IO.FileSystemInfo"/> object 
     56        /// to query its parent.</param> 
     57        /// <returns>The parent directory of the current 
     58        /// <see cref="System.IO.FileSystemInfo"/> object, or null if info is 
     59        /// aleady the root</returns> 
     60        public static DirectoryInfo GetParent(this FileSystemInfo info) 
     61        { 
     62            FileInfo file = info as FileInfo; 
     63            DirectoryInfo directory = info as DirectoryInfo; 
     64 
     65            if (file != null) 
     66                return file.Directory; 
     67            else if (directory != null) 
     68                return directory.Parent; 
     69            else 
     70                throw new ArgumentException("Unknown FileSystemInfo type."); 
     71        } 
     72 
     73        /// <summary> 
     74        /// Moves the provided <see cref="System.IO.FileSystemInfo"/> object 
     75        /// to the provided path. 
     76        /// </summary> 
     77        /// <param name="info">The <see cref="System.IO.FileSystemInfo"/> object 
     78        /// to move.</param> 
     79        /// <param name="path">The path to move the object to.</param> 
     80        public static void MoveTo(this FileSystemInfo info, string path) 
     81        { 
     82            FileInfo file = info as FileInfo; 
     83            DirectoryInfo directory = info as DirectoryInfo; 
     84 
     85            if (file != null) 
     86                file.MoveTo(path); 
     87            else if (directory != null) 
     88                directory.MoveTo(path); 
     89            else 
     90                throw new ArgumentException("Unknown FileSystemInfo type."); 
     91        } 
     92 
     93        /// <summary> 
     94        /// Compacts the file path, fitting in the given width. 
     95        /// </summary> 
     96        /// <param name="info">The <see cref="System.IO.FileSystemObject"/> that should 
     97        /// get a compact path.</param> 
     98        /// <param name="newWidth">The target width of the text.</param> 
     99        /// <param name="drawFont">The font used for drawing the text.</param> 
     100        /// <returns>The compacted file path.</returns> 
     101        public static string GetCompactPath(this FileSystemInfo info, int newWidth, Font drawFont) 
     102        { 
     103            using (Control ctrl = new Control()) 
     104            { 
     105                //First check if the source string is too long. 
     106                Graphics g = ctrl.CreateGraphics(); 
     107                string longPath = info.FullName; 
     108                int width = g.MeasureString(longPath, drawFont).ToSize().Width; 
     109                if (width <= newWidth) 
     110                    return longPath; 
     111 
     112                //It is, shorten it. 
     113                int aveCharWidth = width / longPath.Length; 
     114                int charCount = newWidth / aveCharWidth; 
     115                StringBuilder builder = new StringBuilder(); 
     116                builder.Append(longPath); 
     117                builder.EnsureCapacity(charCount); 
     118 
     119                while (g.MeasureString(builder.ToString(), drawFont).Width > newWidth) 
     120                { 
     121                    if (!NativeMethods.PathCompactPathEx(builder, longPath, 
     122                        (uint)charCount--, 0)) 
     123                    { 
     124                        return string.Empty; 
     125                    } 
     126                } 
     127 
     128                return builder.ToString(); 
     129            } 
     130        } 
     131 
     132        /// <summary> 
     133        /// Checks whether the path given is compressed. 
     134        /// </summary> 
     135        /// <param name="info">The <see cref="System.IO.FileInfo"/> object</param> 
     136        /// <returns>True if the file or folder is compressed.</returns> 
     137        public static bool IsCompressed(this FileSystemInfo info) 
     138        { 
     139            ushort compressionStatus = 0; 
     140            uint bytesReturned = 0; 
     141 
     142            using (SafeFileHandle handle = NativeMethods.CreateFile(info.FullName, 
     143                NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 
     144                0, IntPtr.Zero, NativeMethods.OPEN_EXISTING, 
     145                NativeMethods.FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero)) 
     146            { 
     147                if (NativeMethods.DeviceIoControl(handle, NativeMethods.FSCTL_GET_COMPRESSION, 
     148                    IntPtr.Zero, 0, out compressionStatus, sizeof(ushort), out bytesReturned, 
     149                    IntPtr.Zero)) 
     150                { 
     151                    return compressionStatus != NativeMethods.COMPRESSION_FORMAT_NONE; 
     152                } 
     153            } 
     154 
     155            return false; 
     156        } 
     157 
     158        /// <summary> 
     159        /// Compresses the given file. 
     160        /// </summary> 
     161        /// <param name="info">The File to compress.</param> 
     162        /// <returns>The success ofthe compression</returns> 
     163        public static bool Compress(this FileSystemInfo info) 
     164        { 
     165            return SetCompression(info.FullName, true); 
     166        } 
     167 
     168        /// <summary> 
     169        /// Uncompresses the given file. 
     170        /// </summary> 
     171        /// <param name="info">The File to uncompress.</param> 
     172        /// <returns>The success ofthe uncompression</returns> 
     173        public static bool Uncompress(this FileSystemInfo info) 
     174        { 
     175            return SetCompression(info.FullName, false); 
     176        } 
     177 
     178        /// <summary> 
     179        /// Sets whether the file system object pointed to by path is compressed. 
     180        /// </summary> 
     181        /// <param name="path">The path to the file or folder.</param> 
     182        /// <returns>True if the file or folder has its compression value set.</returns> 
     183        private static bool SetCompression(string path, bool compressed) 
     184        { 
     185            ushort compressionStatus = compressed ? 
     186                NativeMethods.COMPRESSION_FORMAT_DEFAULT : 
     187                NativeMethods.COMPRESSION_FORMAT_NONE; 
     188            uint bytesReturned = 0; 
     189 
     190            using (SafeFileHandle handle = NativeMethods.CreateFile(path, 
     191                NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 
     192                0, IntPtr.Zero, NativeMethods.OPEN_EXISTING, 
     193                NativeMethods.FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero)) 
     194            { 
     195                return NativeMethods.DeviceIoControl(handle, NativeMethods.FSCTL_SET_COMPRESSION, 
     196                    ref compressionStatus, sizeof(ushort), IntPtr.Zero, 0, out bytesReturned, 
     197                    IntPtr.Zero); 
     198            } 
     199        } 
     200 
     201        /// <summary> 
     202        /// Uses SHGetFileInfo to retrieve the description for the given file, 
     203        /// folder or drive. 
     204        /// </summary> 
     205        /// <param name="info">The file system object to query the description of.</param> 
     206        /// <returns>A string containing the description</returns> 
     207        public static string GetDescription(this FileSystemInfo info) 
     208        { 
     209            NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO(); 
     210            NativeMethods.SHGetFileInfo(info.FullName, 0, ref shfi, Marshal.SizeOf(shfi), 
     211                NativeMethods.SHGetFileInfoFlags.SHGFI_DISPLAYNAME); 
     212            return shfi.szDisplayName; 
     213        } 
     214 
     215        /// <summary> 
     216        /// Uses SHGetFileInfo to retrieve the icon for the given file, folder or 
     217        /// drive. 
     218        /// </summary> 
     219        /// <param name="info">The file system object to query the description of.</param> 
     220        /// <returns>An Icon object containing the bitmap</returns> 
     221        public static Icon GetIcon(this FileSystemInfo info) 
     222        { 
     223            NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO(); 
     224            NativeMethods.SHGetFileInfo(info.FullName, 0, ref shfi, Marshal.SizeOf(shfi), 
     225                NativeMethods.SHGetFileInfoFlags.SHGFI_SMALLICON | 
     226                NativeMethods.SHGetFileInfoFlags.SHGFI_ICON); 
     227 
     228            if (shfi.hIcon != IntPtr.Zero) 
     229                return Icon.FromHandle(shfi.hIcon); 
     230            else 
     231                throw new IOException(string.Format(CultureInfo.CurrentCulture, 
     232                    S._("Could not load file icon from {0}"), info.FullName), 
     233                    Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error())); 
     234        } 
     235 
     236        /// <summary> 
     237        /// Determines if a given file is protected by SFC. 
     238        /// </summary> 
     239        /// <param name="info">The file systme object to check.</param> 
     240        /// <returns>True if the file is protected.</returns> 
     241        public static bool IsProtectedSystemFile(this FileSystemInfo info) 
     242        { 
     243            return NativeMethods.SfcIsFileProtected(IntPtr.Zero, info.FullName); 
     244        } 
     245 
     246        /// <summary> 
    52247        /// Copies an existing file to a new file, allowing the monitoring of the progress 
    53248        /// of the copy operation. 
     
    67262                    NativeMethods.CopyProgressFunctionCallbackReasons dwCallbackReason, 
    68263                    IntPtr hSourceFile, IntPtr hDestinationFile, IntPtr lpData) 
    69                 { 
    70                     return progress(TotalFileSize, TotalBytesTransferred); 
    71                 }; 
     264            { 
     265                return progress(TotalFileSize, TotalBytesTransferred); 
     266            }; 
    72267 
    73268            if (!NativeMethods.CopyFileEx(info.FullName, destFileName, callback, IntPtr.Zero, 
     
    120315            /// </summary> 
    121316            Stop = 2 
    122         } 
    123  
    124         /// <summary> 
    125         /// Gets the parent directory of the current <see cref="System.IO.FileSystemInfo"/> 
    126         /// object. 
    127         /// </summary> 
    128         /// <param name="info">The <see cref="System.IO.FileSystemInfo"/> object 
    129         /// to query its parent.</param> 
    130         /// <returns>The parent directory of the current 
    131         /// <see cref="System.IO.FileSystemInfo"/> object, or null if info is 
    132         /// aleady the root</returns> 
    133         public static DirectoryInfo GetParent(this FileSystemInfo info) 
    134         { 
    135             FileInfo file = info as FileInfo; 
    136             DirectoryInfo directory = info as DirectoryInfo; 
    137  
    138             if (file != null) 
    139                 return file.Directory; 
    140             else if (directory != null) 
    141                 return directory.Parent; 
    142             else 
    143                 throw new ArgumentException("Unknown FileSystemInfo type."); 
    144         } 
    145  
    146         /// <summary> 
    147         /// Moves the provided <see cref="System.IO.FileSystemInfo"/> object 
    148         /// to the provided path. 
    149         /// </summary> 
    150         /// <param name="info">The <see cref="System.IO.FileSystemInfo"/> object 
    151         /// to move.</param> 
    152         /// <param name="path">The path to move the object to.</param> 
    153         public static void MoveTo(this FileSystemInfo info, string path) 
    154         { 
    155             FileInfo file = info as FileInfo; 
    156             DirectoryInfo directory = info as DirectoryInfo; 
    157  
    158             if (file != null) 
    159                 file.MoveTo(path); 
    160             else if (directory != null) 
    161                 directory.MoveTo(path); 
    162             else 
    163                 throw new ArgumentException("Unknown FileSystemInfo type."); 
    164         } 
    165  
    166         /// <summary> 
    167         /// Compacts the file path, fitting in the given width. 
    168         /// </summary> 
    169         /// <param name="info">The <see cref="System.IO.FileSystemObject"/> that should 
    170         /// get a compact path.</param> 
    171         /// <param name="newWidth">The target width of the text.</param> 
    172         /// <param name="drawFont">The font used for drawing the text.</param> 
    173         /// <returns>The compacted file path.</returns> 
    174         public static string GetCompactPath(this FileSystemInfo info, int newWidth, Font drawFont) 
    175         { 
    176             using (Control ctrl = new Control()) 
    177             { 
    178                 //First check if the source string is too long. 
    179                 Graphics g = ctrl.CreateGraphics(); 
    180                 string longPath = info.FullName; 
    181                 int width = g.MeasureString(longPath, drawFont).ToSize().Width; 
    182                 if (width <= newWidth) 
    183                     return longPath; 
    184  
    185                 //It is, shorten it. 
    186                 int aveCharWidth = width / longPath.Length; 
    187                 int charCount = newWidth / aveCharWidth; 
    188                 StringBuilder builder = new StringBuilder(); 
    189                 builder.Append(longPath); 
    190                 builder.EnsureCapacity(charCount); 
    191  
    192                 while (g.MeasureString(builder.ToString(), drawFont).Width > newWidth) 
    193                 { 
    194                     if (!NativeMethods.PathCompactPathEx(builder, longPath, 
    195                         (uint)charCount--, 0)) 
    196                     { 
    197                         return string.Empty; 
    198                     } 
    199                 } 
    200  
    201                 return builder.ToString(); 
    202             } 
    203         } 
    204  
    205         /// <summary> 
    206         /// Checks whether the path given is compressed. 
    207         /// </summary> 
    208         /// <param name="info">The <see cref="System.IO.FileInfo"/> object</param> 
    209         /// <returns>True if the file or folder is compressed.</returns> 
    210         public static bool IsCompressed(this FileSystemInfo info) 
    211         { 
    212             ushort compressionStatus = 0; 
    213             uint bytesReturned = 0; 
    214  
    215             using (SafeFileHandle handle = NativeMethods.CreateFile(info.FullName, 
    216                 NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 
    217                 0, IntPtr.Zero, NativeMethods.OPEN_EXISTING, 
    218                 NativeMethods.FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero)) 
    219             { 
    220                 if (NativeMethods.DeviceIoControl(handle, NativeMethods.FSCTL_GET_COMPRESSION, 
    221                     IntPtr.Zero, 0, out compressionStatus, sizeof(ushort), out bytesReturned, 
    222                     IntPtr.Zero)) 
    223                 { 
    224                     return compressionStatus != NativeMethods.COMPRESSION_FORMAT_NONE; 
    225                 } 
    226             } 
    227  
    228             return false; 
    229         } 
    230  
    231         /// <summary> 
    232         /// Compresses the given file. 
    233         /// </summary> 
    234         /// <param name="info">The File to compress.</param> 
    235         /// <returns>The success ofthe compression</returns> 
    236         public static bool Compress(this FileSystemInfo info) 
    237         { 
    238             return SetCompression(info.FullName, true); 
    239         } 
    240  
    241         /// <summary> 
    242         /// Uncompresses the given file. 
    243         /// </summary> 
    244         /// <param name="info">The File to uncompress.</param> 
    245         /// <returns>The success ofthe uncompression</returns> 
    246         public static bool Uncompress(this FileSystemInfo info) 
    247         { 
    248             return SetCompression(info.FullName, false); 
    249         } 
    250  
    251         /// <summary> 
    252         /// Sets whether the file system object pointed to by path is compressed. 
    253         /// </summary> 
    254         /// <param name="path">The path to the file or folder.</param> 
    255         /// <returns>True if the file or folder has its compression value set.</returns> 
    256         private static bool SetCompression(string path, bool compressed) 
    257         { 
    258             ushort compressionStatus = compressed ? 
    259                 NativeMethods.COMPRESSION_FORMAT_DEFAULT : 
    260                 NativeMethods.COMPRESSION_FORMAT_NONE; 
    261             uint bytesReturned = 0; 
    262  
    263             using (SafeFileHandle handle = NativeMethods.CreateFile(path, 
    264                 NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 
    265                 0, IntPtr.Zero, NativeMethods.OPEN_EXISTING, 
    266                 NativeMethods.FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero)) 
    267             { 
    268                 return NativeMethods.DeviceIoControl(handle, NativeMethods.FSCTL_SET_COMPRESSION, 
    269                     ref compressionStatus, sizeof(ushort), IntPtr.Zero, 0, out bytesReturned, 
    270                     IntPtr.Zero); 
    271             } 
    272         } 
    273  
    274         /// <summary> 
    275         /// Uses SHGetFileInfo to retrieve the description for the given file, 
    276         /// folder or drive. 
    277         /// </summary> 
    278         /// <param name="info">The file system object to query the description of.</param> 
    279         /// <returns>A string containing the description</returns> 
    280         public static string GetDescription(this FileSystemInfo info) 
    281         { 
    282             NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO(); 
    283             NativeMethods.SHGetFileInfo(info.FullName, 0, ref shfi, Marshal.SizeOf(shfi), 
    284                 NativeMethods.SHGetFileInfoFlags.SHGFI_DISPLAYNAME); 
    285             return shfi.szDisplayName; 
    286         } 
    287  
    288         /// <summary> 
    289         /// Uses SHGetFileInfo to retrieve the icon for the given file, folder or 
    290         /// drive. 
    291         /// </summary> 
    292         /// <param name="info">The file system object to query the description of.</param> 
    293         /// <returns>An Icon object containing the bitmap</returns> 
    294         public static Icon GetIcon(this FileSystemInfo info) 
    295         { 
    296             NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO(); 
    297             NativeMethods.SHGetFileInfo(info.FullName, 0, ref shfi, Marshal.SizeOf(shfi), 
    298                 NativeMethods.SHGetFileInfoFlags.SHGFI_SMALLICON | 
    299                 NativeMethods.SHGetFileInfoFlags.SHGFI_ICON); 
    300  
    301             if (shfi.hIcon != IntPtr.Zero) 
    302                 return Icon.FromHandle(shfi.hIcon); 
    303             else 
    304                 throw new IOException(string.Format(CultureInfo.CurrentCulture, 
    305                     S._("Could not load file icon from {0}"), info.FullName), 
    306                     Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error())); 
    307         } 
    308  
    309         /// <summary> 
    310         /// Determines if a given file is protected by SFC. 
    311         /// </summary> 
    312         /// <param name="info">The file systme object to check.</param> 
    313         /// <returns>True if the file is protected.</returns> 
    314         public static bool IsProtectedSystemFile(this FileSystemInfo info) 
    315         { 
    316             return NativeMethods.SfcIsFileProtected(IntPtr.Zero, info.FullName); 
    317317        } 
    318318 
Note: See TracChangeset for help on using the changeset viewer.