/// <summary> /// Creates a file stream with the given filename that is deleted when either the /// stream is closed, or the application is terminated. /// </summary> /// <remarks> /// If the file already exists, it is overwritten without any error (CREATE_ALWAYS). /// </remarks> /// <param name="fileName">The full path to the file to create.</param> /// <returns>A Stream with read and write access.</returns> public static FileStream CreateTempFile(string fileName) { IntPtr hFile = SafeNativeMethods.CreateFileW( fileName, NativeConstants.GENERIC_READ | NativeConstants.GENERIC_WRITE, NativeConstants.FILE_SHARE_READ, IntPtr.Zero, NativeConstants.CREATE_ALWAYS, NativeConstants.FILE_ATTRIBUTE_TEMPORARY | NativeConstants.FILE_FLAG_DELETE_ON_CLOSE | NativeConstants.FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, IntPtr.Zero); if (hFile == NativeConstants.INVALID_HANDLE_VALUE) { NativeMethods.ThrowOnWin32Error("CreateFileW returned INVALID_HANDLE_VALUE"); } SafeFileHandle sfhFile = new SafeFileHandle(hFile, true); FileStream stream; try { stream = new FileStream(sfhFile, FileAccess.ReadWrite); } catch (Exception) { SafeNativeMethods.CloseHandle(hFile); hFile = IntPtr.Zero; throw; } return(stream); }
private static bool EnableCompression(string filePath) { IntPtr hFile = IntPtr.Zero; try { hFile = SafeNativeMethods.CreateFileW( filePath, NativeConstants.GENERIC_READ | NativeConstants.GENERIC_WRITE, NativeConstants.FILE_SHARE_READ | NativeConstants.FILE_SHARE_WRITE | NativeConstants.FILE_SHARE_DELETE, IntPtr.Zero, NativeConstants.OPEN_EXISTING, NativeConstants.FILE_FLAG_BACKUP_SEMANTICS, IntPtr.Zero); if (hFile == NativeConstants.INVALID_HANDLE_VALUE) { int dwError = Marshal.GetLastWin32Error(); return(false); } ushort cType = NativeConstants.COMPRESSION_FORMAT_DEFAULT; uint dwBytes = 0; bool bResult; unsafe { bResult = NativeMethods.DeviceIoControl( hFile, NativeConstants.FSCTL_SET_COMPRESSION, new IntPtr(&cType), sizeof(ushort), IntPtr.Zero, 0, ref dwBytes, IntPtr.Zero); } return(bResult); } finally { if (hFile != IntPtr.Zero) { SafeNativeMethods.CloseHandle(hFile); hFile = IntPtr.Zero; } } }
/// <summary> /// Opens a file for streaming. This stream should be read from or written /// to sequentially for best performance. Random I/O is still permissible, /// but may not perform as well. /// This file is created in such a way that is it NOT indexed by the system's /// file indexer (e.g., Windows Desktop Search). /// </summary> /// <param name="fileName">The file to open.</param> /// <returns>A Stream object that may be used to read from or write to the file, depending on the fileMode parameter.</returns> public static FileStream OpenStreamingFile(string fileName, FileAccess fileAccess) { uint dwDesiredAccess; uint dwCreationDisposition; switch (fileAccess) { case FileAccess.Read: dwDesiredAccess = NativeConstants.GENERIC_READ; dwCreationDisposition = NativeConstants.OPEN_EXISTING; break; case FileAccess.ReadWrite: dwDesiredAccess = NativeConstants.GENERIC_READ | NativeConstants.GENERIC_WRITE; dwCreationDisposition = NativeConstants.OPEN_ALWAYS; break; case FileAccess.Write: dwDesiredAccess = NativeConstants.GENERIC_WRITE; dwCreationDisposition = NativeConstants.CREATE_NEW; break; default: throw new InvalidEnumArgumentException(); } uint dwFlagsAndAttributes = NativeConstants.FILE_ATTRIBUTE_TEMPORARY | NativeConstants.FILE_FLAG_SEQUENTIAL_SCAN | NativeConstants.FILE_ATTRIBUTE_NOT_CONTENT_INDEXED; IntPtr hFile = SafeNativeMethods.CreateFileW( fileName, dwDesiredAccess, NativeConstants.FILE_SHARE_READ, IntPtr.Zero, dwCreationDisposition, dwFlagsAndAttributes, IntPtr.Zero); if (hFile == NativeConstants.INVALID_HANDLE_VALUE) { NativeMethods.ThrowOnWin32Error("CreateFileW returned INVALID_HANDLE_VALUE"); } FileStream stream; try { SafeFileHandle sfh = new SafeFileHandle(hFile, true); stream = new FileStream(sfh, fileAccess, 512, false); } catch { SafeNativeMethods.CloseHandle(hFile); hFile = IntPtr.Zero; throw; } return(stream); }