Changeset 1548


Ignore:
Timestamp:
1/18/2010 4:51:59 AM (5 years ago)
Author:
lowjoel
Message:

Reviewed File.cs to get it to compile after the NativeMethods? refactor. Create a new file Sfc.cs for SfcIsFileProtected?. Addresses #284: Eraser.Util rewrite

Location:
branches/eraser6/CodeReview/Eraser.Util
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/CodeReview/Eraser.Util/Eraser.Util.csproj

    r1543 r1548  
    8383    </CodeAnalysisDictionary> 
    8484  </ItemGroup> 
     85  <ItemGroup> 
     86    <Compile Include="NativeMethods\Sfc.cs" /> 
     87  </ItemGroup> 
    8588  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> 
    8689  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.  
  • branches/eraser6/CodeReview/Eraser.Util/File.cs

    r1528 r1548  
    5050            { 
    5151                //Allocate the structures 
    52                 NTApi.NativeMethods.FILE_STREAM_INFORMATION[] streams = 
    53                     NTApi.NativeMethods.NtQueryInformationFile(streamHandle); 
    54  
    55                 foreach (NTApi.NativeMethods.FILE_STREAM_INFORMATION streamInfo in streams) 
     52                NativeMethods.FILE_STREAM_INFORMATION[] streams = 
     53                    NTApi.NtQueryInformationFile(streamHandle); 
     54 
     55                foreach (NativeMethods.FILE_STREAM_INFORMATION streamInfo in streams) 
    5656                { 
    5757                    //Get the name of the stream. The raw value is :NAME:$DATA 
     
    7777        public static string GetFileDescription(string path) 
    7878        { 
    79             ShellApi.NativeMethods.SHFILEINFO shfi = new ShellApi.NativeMethods.SHFILEINFO(); 
    80             ShellApi.NativeMethods.SHGetFileInfo(path, 0, ref shfi, Marshal.SizeOf(shfi), 
    81                 ShellApi.NativeMethods.SHGetFileInfoFlags.SHGFI_DISPLAYNAME); 
     79            NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO(); 
     80            NativeMethods.SHGetFileInfo(path, 0, ref shfi, Marshal.SizeOf(shfi), 
     81                NativeMethods.SHGetFileInfoFlags.SHGFI_DISPLAYNAME); 
    8282            return shfi.szDisplayName; 
    8383        } 
     
    9393        public static Icon GetFileIcon(string path) 
    9494        { 
    95             ShellApi.NativeMethods.SHFILEINFO shfi = new ShellApi.NativeMethods.SHFILEINFO(); 
    96             ShellApi.NativeMethods.SHGetFileInfo(path, 0, ref shfi, Marshal.SizeOf(shfi), 
    97                 ShellApi.NativeMethods.SHGetFileInfoFlags.SHGFI_SMALLICON | 
    98                 ShellApi.NativeMethods.SHGetFileInfoFlags.SHGFI_ICON); 
     95            NativeMethods.SHFILEINFO shfi = new NativeMethods.SHFILEINFO(); 
     96            NativeMethods.SHGetFileInfo(path, 0, ref shfi, Marshal.SizeOf(shfi), 
     97                NativeMethods.SHGetFileInfoFlags.SHGFI_SMALLICON | 
     98                NativeMethods.SHGetFileInfoFlags.SHGFI_ICON); 
    9999 
    100100            if (shfi.hIcon != IntPtr.Zero) 
     
    132132                while (g.MeasureString(builder.ToString(), drawFont).Width > newWidth) 
    133133                { 
    134                     if (!ShellApi.NativeMethods.PathCompactPathEx(builder, longPath, 
     134                    if (!NativeMethods.PathCompactPathEx(builder, longPath, 
    135135                        (uint)charCount--, 0)) 
    136136                    { 
     
    150150        public static bool IsProtectedSystemFile(string filePath) 
    151151        { 
    152             return SfcIsFileProtected(IntPtr.Zero, filePath); 
     152            return NativeMethods.SfcIsFileProtected(IntPtr.Zero, filePath); 
    153153        } 
    154154 
     
    163163            uint bytesReturned = 0; 
    164164 
    165             using (FileStream strm = new FileStream( 
    166                 KernelApi.NativeMethods.CreateFile(path, 
    167                 KernelApi.NativeMethods.GENERIC_READ | KernelApi.NativeMethods.GENERIC_WRITE, 
    168                 0, IntPtr.Zero, KernelApi.NativeMethods.OPEN_EXISTING, 
    169                 KernelApi.NativeMethods.FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero), FileAccess.Read)) 
    170             { 
    171                 if (KernelApi.NativeMethods.DeviceIoControl(strm.SafeFileHandle, 
    172                     KernelApi.NativeMethods.FSCTL_GET_COMPRESSION, IntPtr.Zero, 0, 
     165            using (FileStream strm = new FileStream(NativeMethods.CreateFile(path, 
     166                NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 
     167                0, IntPtr.Zero, NativeMethods.OPEN_EXISTING, 
     168                NativeMethods.FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero), FileAccess.Read)) 
     169            { 
     170                if (NativeMethods.DeviceIoControl(strm.SafeFileHandle, 
     171                    NativeMethods.FSCTL_GET_COMPRESSION, IntPtr.Zero, 0, 
    173172                    out compressionStatus, sizeof(ushort), out bytesReturned, IntPtr.Zero)) 
    174173                { 
    175                     return compressionStatus != KernelApi.NativeMethods.COMPRESSION_FORMAT_NONE; 
     174                    return compressionStatus != NativeMethods.COMPRESSION_FORMAT_NONE; 
    176175                } 
    177176            } 
     
    188187        { 
    189188            ushort compressionStatus = compressed ? 
    190                 KernelApi.NativeMethods.COMPRESSION_FORMAT_DEFAULT : 
    191                 KernelApi.NativeMethods.COMPRESSION_FORMAT_NONE; 
     189                NativeMethods.COMPRESSION_FORMAT_DEFAULT : 
     190                NativeMethods.COMPRESSION_FORMAT_NONE; 
    192191            uint bytesReturned = 0; 
    193192 
    194             using (FileStream strm = new FileStream( 
    195                 KernelApi.NativeMethods.CreateFile(path, 
    196                 KernelApi.NativeMethods.GENERIC_READ | KernelApi.NativeMethods.GENERIC_WRITE, 
    197                 0, IntPtr.Zero, KernelApi.NativeMethods.OPEN_EXISTING, 
    198                 KernelApi.NativeMethods.FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero), FileAccess.ReadWrite)) 
    199             { 
    200                 return KernelApi.NativeMethods.DeviceIoControl(strm.SafeFileHandle, 
    201                     KernelApi.NativeMethods.FSCTL_SET_COMPRESSION, ref compressionStatus, 
     193            using (FileStream strm = new FileStream(NativeMethods.CreateFile(path, 
     194                NativeMethods.GENERIC_READ | NativeMethods.GENERIC_WRITE, 
     195                0, IntPtr.Zero, NativeMethods.OPEN_EXISTING, 
     196                NativeMethods.FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero), FileAccess.ReadWrite)) 
     197            { 
     198                return NativeMethods.DeviceIoControl(strm.SafeFileHandle, 
     199                    NativeMethods.FSCTL_SET_COMPRESSION, ref compressionStatus, 
    202200                    sizeof(ushort), IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero); 
    203201            } 
    204202        } 
    205  
    206         /// <summary> 
    207         /// Determines whether the specified file is protected. Applications 
    208         /// should avoid replacing protected system files. 
    209         /// </summary> 
    210         /// <param name="RpcHandle">This parameter must be NULL.</param> 
    211         /// <param name="ProtFileName">The name of the file.</param> 
    212         /// <returns>If the file is protected, the return value is true.</returns> 
    213         [DllImport("Sfc.dll", CharSet = CharSet.Unicode)] 
    214         [return: MarshalAs(UnmanagedType.Bool)] 
    215         private static extern bool SfcIsFileProtected(IntPtr RpcHandle, 
    216             string ProtFileName); 
    217203 
    218204        /// <summary> 
Note: See TracChangeset for help on using the changeset viewer.