Exemplo n.º 1
0
        internal static IntPtr GetHandleOnFile(string filePath, uint access)
        {
            IntPtr _handle;

            _handle = NativeDisk.CreateFileW(filePath, access, (access == NativeDisk.GENERIC_READ) ? NativeDisk.FILE_SHARE_READ : 0U, IntPtr.Zero, access == NativeDisk.GENERIC_READ ? NativeDisk.OPEN_EXISTING : NativeDisk.CREATE_ALWAYS, 0, IntPtr.Zero);

            if (_handle == NativeDisk.INVALID_HANDLE_VALUE)
            {
                var exception = new Win32Exception(Marshal.GetLastWin32Error());
                throw new Exception(string.Format("Error occured when trying to get handle on file.\nError code: {0}\nMessage: {1}", exception.NativeErrorCode, exception.Message));
            }

            return(_handle);
        }