public Win32MemoryMapPager(string file, NativeFileAttributes options = NativeFileAttributes.Normal, NativeFileAccess access = NativeFileAccess.GenericRead | NativeFileAccess.GenericWrite) { _access = access; _fileInfo = new FileInfo(file); bool noData = _fileInfo.Exists == false || _fileInfo.Length == 0; _handle = NativeFileMethods.CreateFile(file, access, NativeFileShare.Read | NativeFileShare.Write | NativeFileShare.Delete, IntPtr.Zero, NativeFileCreationDisposition.OpenAlways, options, IntPtr.Zero); if (_handle.IsInvalid) { int lastWin32ErrorCode = Marshal.GetLastWin32Error(); throw new IOException("Failed to open file storage of Win32MemoryMapPager", new Win32Exception(lastWin32ErrorCode)); } _fileStream = new FileStream(_handle, FileAccess.ReadWrite); if (noData) { NumberOfAllocatedPages = 0; } else { NumberOfAllocatedPages = _fileInfo.Length/PageSize; PagerState.Release(); PagerState = CreateNewPagerState(); } }
public Win32MemoryMapPager(string file, NativeFileAttributes options = NativeFileAttributes.Normal, NativeFileAccess access = NativeFileAccess.GenericRead | NativeFileAccess.GenericWrite) { _access = access; _fileInfo = new FileInfo(file); bool noData = _fileInfo.Exists == false || _fileInfo.Length == 0; _handle = NativeFileMethods.CreateFile(file, access, NativeFileShare.Read | NativeFileShare.Write | NativeFileShare.Delete, IntPtr.Zero, NativeFileCreationDisposition.OpenAlways, options, IntPtr.Zero); if (_handle.IsInvalid) { int lastWin32ErrorCode = Marshal.GetLastWin32Error(); throw new IOException("Failed to open file storage of Win32MemoryMapPager", new Win32Exception(lastWin32ErrorCode)); } _fileStream = new FileStream(_handle, FileAccess.ReadWrite); if (noData) { NumberOfAllocatedPages = 0; } else { NumberOfAllocatedPages = _fileInfo.Length / PageSize; PagerState.Release(); PagerState = CreateNewPagerState(); } }
private static extern SafeFileHandle CreateFile( [In] string fileName, [MarshalAs(UnmanagedType.U4)] NativeFileAccess desiredAccess, FileShare shareMode, [In] IntPtr securityAttributes, [MarshalAs(UnmanagedType.U4)] FileMode creationDisposition, [MarshalAs(UnmanagedType.U4)] NativeFileAttributes flagsAndAttributes, [In] IntPtr templateFile);
public Win32MemoryMapPager(string file, long?initialFileSize = null, NativeFileAttributes options = NativeFileAttributes.Normal, NativeFileAccess access = NativeFileAccess.GenericRead | NativeFileAccess.GenericWrite) { NativeMethods.SYSTEM_INFO systemInfo; NativeMethods.GetSystemInfo(out systemInfo); AllocationGranularity = systemInfo.allocationGranularity; _access = access; _memoryMappedFileAccess = _access == NativeFileAccess.GenericRead ? MemoryMappedFileAccess.Read : MemoryMappedFileAccess.ReadWrite; _handle = NativeFileMethods.CreateFile(file, access, NativeFileShare.Read | NativeFileShare.Write | NativeFileShare.Delete, IntPtr.Zero, NativeFileCreationDisposition.OpenAlways, options, IntPtr.Zero); if (_handle.IsInvalid) { int lastWin32ErrorCode = Marshal.GetLastWin32Error(); throw new IOException("Failed to open file storage of Win32MemoryMapPager", new Win32Exception(lastWin32ErrorCode)); } _fileInfo = new FileInfo(file); var streamAccessType = _access == NativeFileAccess.GenericRead ? FileAccess.Read : FileAccess.ReadWrite; _fileStream = new FileStream(_handle, streamAccessType); _totalAllocationSize = _fileInfo.Length; if (_access.HasFlag(NativeFileAccess.GenericWrite) || _access.HasFlag(NativeFileAccess.GenericAll) || _access.HasFlag(NativeFileAccess.FILE_GENERIC_WRITE)) { var fileLength = _fileStream.Length; if (fileLength == 0 && initialFileSize.HasValue) { fileLength = initialFileSize.Value; } if (_fileStream.Length == 0 || (fileLength % AllocationGranularity != 0)) { fileLength = NearestSizeToAllocationGranularity(fileLength); _fileStream.SetLength(fileLength); } _totalAllocationSize = fileLength; } NumberOfAllocatedPages = _totalAllocationSize / PageSize; PagerState.Release(); PagerState = CreatePagerState(); }
public Win32MemoryMapPager(string file, long? initialFileSize = null, NativeFileAttributes options = NativeFileAttributes.Normal, NativeFileAccess access = NativeFileAccess.GenericRead | NativeFileAccess.GenericWrite) { NativeMethods.SYSTEM_INFO systemInfo; NativeMethods.GetSystemInfo(out systemInfo); AllocationGranularity = systemInfo.allocationGranularity; _access = access; _memoryMappedFileAccess = _access == NativeFileAccess.GenericRead ? MemoryMappedFileAccess.Read : MemoryMappedFileAccess.ReadWrite; _handle = NativeFileMethods.CreateFile(file, access, NativeFileShare.Read | NativeFileShare.Write | NativeFileShare.Delete, IntPtr.Zero, NativeFileCreationDisposition.OpenAlways, options, IntPtr.Zero); if (_handle.IsInvalid) { int lastWin32ErrorCode = Marshal.GetLastWin32Error(); throw new IOException("Failed to open file storage of Win32MemoryMapPager", new Win32Exception(lastWin32ErrorCode)); } _fileInfo = new FileInfo(file); var streamAccessType = _access == NativeFileAccess.GenericRead ? FileAccess.Read : FileAccess.ReadWrite; _fileStream = new FileStream(_handle, streamAccessType); _totalAllocationSize = _fileInfo.Length; if (_access.HasFlag(NativeFileAccess.GenericWrite) || _access.HasFlag(NativeFileAccess.GenericAll) || _access.HasFlag(NativeFileAccess.FILE_GENERIC_WRITE)) { var fileLength = _fileStream.Length; if (fileLength == 0 && initialFileSize.HasValue) fileLength = initialFileSize.Value; if (_fileStream.Length == 0 || (fileLength % AllocationGranularity != 0)) { fileLength = NearestSizeToAllocationGranularity(fileLength); _fileStream.SetLength(fileLength); } _totalAllocationSize = fileLength; } NumberOfAllocatedPages = _totalAllocationSize / PageSize; PagerState.Release(); PagerState = CreatePagerState(); }
public static extern SafeFileHandle CreateFile(string lpFileName, NativeFileAccess dwDesiredAccess, NativeFileShare dwShareMode, IntPtr lpSecurityAttributes, NativeFileCreationDisposition dwCreationDisposition, NativeFileAttributes dwFlagsAndAttributes, IntPtr hTemplateFile);