private void OpenFile(string fileName) { if (!File.Exists(fileName)) { throw new FileNotFoundException(); } IntPtr fileHandle = UnManageAPI.CreateFile(fileName, UnManageAPI.GENERIC_READ | UnManageAPI.GENERIC_WRITE, FileShare.Read | FileShare.Write, IntPtr.Zero, FileMode.Open, UnManageAPI.FILE_ATTRIBUTE_NORMAL | UnManageAPI.FILE_FLAG_SEQUENTIAL_SCAN, IntPtr.Zero); if (UnManageAPI.INVALID_HANDLE_VALUE == (int)fileHandle) { throw new FileLoadException("文件打开出错"); } uint fileSizeHigh = 0; //读取文件大小 long fileSize = UnManageAPI.GetFileSize(fileHandle, out fileSizeHigh); IntPtr mappingFileHandle = UnManageAPI.CreateFileMapping(fileHandle, IntPtr.Zero, UnManageAPI.PAGE_READWRITE, 0, 0, "KugarMapping"); if (mappingFileHandle == IntPtr.Zero) { throw new FileLoadException("文件映射出错"); } fileSize |= (((uint)fileSizeHigh) << 32); UnManageAPI.CloseHandle(fileHandle); targetMappingFileHandle = mappingFileHandle; fileLength = fileSize; }
protected void Disposed(bool disposing) { if (_isDispose) { return; } if (disposing) { UnManageAPI.CloseHandle(targetMappingFileHandle); _isDispose = true; } }