public void Dispose(bool disposing) { lock (this) { if (!m_Disposed) { if (disposing) { if (m_QuotaFileStream != null) { m_QuotaFileStream.Dispose(); m_QuotaFileStream = null; } if (m_UsedSizeFileStream != null) { m_UsedSizeFileStream.Dispose(); m_UsedSizeFileStream = null; } if (m_QuotaFileMapping != null) { m_QuotaFileMapping.Dispose(); m_QuotaFileMapping = null; } if (m_UsedSizeFileMapping != null) { m_UsedSizeFileMapping.Dispose(); m_UsedSizeFileMapping = null; } } if (m_QuotaView != IntPtr.Zero) { Win32Native.UnmapViewOfFile(m_QuotaView); m_QuotaView = IntPtr.Zero; } if (m_UsedSizeView != IntPtr.Zero) { Win32Native.UnmapViewOfFile(m_UsedSizeView); m_UsedSizeView = IntPtr.Zero; } m_Disposed = true; } } }
/// <summary>Cleans up resources used by the <see cref="FileMapping" /> class.</summary> /// <param name="disposing"> /// <see langword="true" /> to clean up both managed and unmanaged resources; /// <see langword="false" /> to clean up only unmanaged resources. /// </param> private void Dispose(bool disposing) { if (!_disposed) { if (_view != IntPtr.Zero) { NativeMethods.UnmapViewOfFile(_view); } if (disposing) { _mapping?.Dispose(); _file?.Dispose(); } _disposed = true; } }
/// <summary>Cleans up resources used by the <see cref="FileMapping"/> class.</summary> /// <param name="disposing"><see langword="true"/> to clean up both managed and unmanaged resources; <see langword="false" /> to clean up only unmanaged resources.</param> private void Dispose(Boolean disposing) { if (!_disposed) { if (_view != IntPtr.Zero) { NativeMethods.UnmapViewOfFile(_view); } if (disposing) { if (_mapping != null) { _mapping.Dispose(); } if (_file != null) { _file.Dispose(); } } _disposed = true; } }
/// <summary>Initializes a new instance of the <see cref="FileMapping" /> class.</summary> /// <param name="fileName">The file that should be memory-mapped</param> public FileMapping(string fileName, FileMapMode mode) { if (fileName == null) { throw new ArgumentNullException(nameof(fileName)); } var fileMode = FileMode.Open; FileAccess fileAccess; FileShare fileShare; FileMapProtection mapProtection; FileMapAccess mapAccess; switch (mode) { case FileMapMode.Read: fileAccess = FileAccess.Read | GenericRead; fileShare = FileShare.Read; mapProtection = FileMapProtection.PageReadOnly; mapAccess = FileMapAccess.FileMapRead; break; case FileMapMode.Write: fileAccess = FileAccess.Write | GenericWrite; fileShare = FileShare.None; mapProtection = FileMapProtection.PageWriteCopy; mapAccess = FileMapAccess.FileMapWrite; break; case FileMapMode.ReadWrite: default: fileAccess = FileAccess.ReadWrite | GenericRead | GenericWrite; fileShare = FileShare.None; mapProtection = FileMapProtection.PageReadWrite; mapAccess = FileMapAccess.FileMapAllAccess; break; } // Get the size. // This throws a proper FileNotFoundException if the file doesn't exist so we don't need to check that anymore. _size = new FileInfo(fileName).Length; try { _file = NativeMethods.CreateFile( fileName, fileAccess, fileShare, IntPtr.Zero, fileMode, FileAttributes.Normal, IntPtr.Zero ); if (_file.IsInvalid) { throw new Win32Exception(); } _mapping = NativeMethods.CreateFileMapping(_file, IntPtr.Zero, mapProtection, 0, 0, null); if (_mapping.IsInvalid) { throw new Win32Exception(); } _view = NativeMethods.MapViewOfFile(_mapping, mapAccess, 0, 0, IntPtr.Zero); if (_view == IntPtr.Zero) { throw new Win32Exception(); } } catch { if (_view != IntPtr.Zero) { NativeMethods.UnmapViewOfFile(_view); } _mapping?.Dispose(); _file?.Dispose(); throw; } }