コード例 #1
0
        private static SafeHeapBlockHandle ReadFileWorker(SlimFileInfo fileInfo, int trailingByteCount)
        {
            using (
                var fileHandle = NativeMethods.CreateFile(fileInfo.FullPathName.FullName, NativeAccessFlags.GenericRead, FileShare.Read, IntPtr.Zero,
                                                          FileMode.Open, 0, IntPtr.Zero)) {
                if (fileHandle.IsInvalid)
                {
                    throw new Win32Exception();
                }

                // Note: We are limited to 2GB files by design.
                var len       = (int)fileInfo.Length;
                var heap      = HeapAllocStatic.Alloc(len + trailingByteCount);
                var bytesRead = new int[1];

                if (!NativeMethods.ReadFile(fileHandle, heap.Pointer, len, bytesRead, null))
                {
                    throw new Win32Exception();
                }

                if (bytesRead[0] != len)
                {
                    throw new Exception("File read operation didn't read the whole file");
                }

                return(heap);
            }
        }
コード例 #2
0
        private static SafeHeapBlockHandle ReadFileWorker(SlimFileInfo fileInfo, int trailingByteCount)
        {
            using (
                var fileHandle = NativeMethods.CreateFile(fileInfo.FullPath.Value, NativeAccessFlags.GenericRead, FileShare.Read, IntPtr.Zero,
                                                          FileMode.Open, 0, IntPtr.Zero)) {
                if (fileHandle.IsInvalid)
                {
                    throw new Win32Exception();
                }

                // Note: We are limited to 2GB files by design.
                int maxLen = Int32.MaxValue - trailingByteCount;
                if (fileInfo.Length >= maxLen)
                {
                    Logger.LogWarning("File too big, truncated to {0} bytes", maxLen);
                }
                var len       = (int)Math.Min(maxLen, fileInfo.Length);
                var heap      = HeapAllocStatic.Alloc(len + trailingByteCount);
                var bytesRead = new int[1];

                if (!NativeMethods.ReadFile(fileHandle, heap.Pointer, len, bytesRead, null))
                {
                    throw new Win32Exception();
                }

                if (bytesRead[0] != len)
                {
                    throw new Exception("File read operation didn't read the whole file");
                }

                return(heap);
            }
        }