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(); } }
internal static extern IntPtr Create( string fileName, NativeFileAccess desiredAccess, NativeFileShare shareMode, IntPtr securityAttributes, NativeFileMode mode, NativeFileOptions flagsAndOptions, IntPtr templateFile);
public static extern SafeFileHandle CreateFile( string filename, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileFlags flags, IntPtr template );
/// <summary> /// Initializes a new instance of the <see cref="NativeFileStream"/> class. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="fileMode">The file mode.</param> /// <param name="access">The access mode.</param> /// <param name="share">The share mode.</param> public unsafe NativeFileStream(string fileName, NativeFileMode fileMode, NativeFileAccess access, NativeFileShare share = NativeFileShare.Read) { #if W8CORE //uint newAccess = 0; //const int FILE_ATTRIBUTE_NORMAL = 0x00000080; //const int FILE_FLAG_RANDOM_ACCESS = 0x10000000; //const int FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000; //var extendedParams = default(NativeFile.CREATEFILE2_EXTENDED_PARAMETERS); //extendedParams.dwSize = (uint)Utilities.SizeOf<NativeFile.CREATEFILE2_EXTENDED_PARAMETERS>(); //extendedParams.dwFileAttributes = FILE_ATTRIBUTE_NORMAL; //extendedParams.dwFileFlags = FILE_FLAG_RANDOM_ACCESS; //extendedParams.dwSecurityQosFlags = 0; //extendedParams.lpSecurityAttributes = IntPtr.Zero; //extendedParams.hTemplateFile = IntPtr.Zero; //if ((access & NativeFileAccess.Read) != 0) //{ // // Sets GENERIC_READ // newAccess |= 0x00120089; //} //if ((access & NativeFileAccess.Write) != 0) //{ // newAccess |= 0x00120116; //} //if ((access & NativeFileAccess.Execute) != 0) //{ // newAccess |= 0x001200a0; //} //handle = NativeFile.Create(fileName, (NativeFileAccess)newAccess, share, fileMode, new IntPtr(&extendedParams)); handle = NativeFile.Create(fileName, access, share, fileMode, IntPtr.Zero); #else handle = NativeFile.Create(fileName, access, share, IntPtr.Zero, fileMode, NativeFileOptions.None, IntPtr.Zero); #endif if (handle == new IntPtr(-1)) { var lastWin32Error = MarshalGetLastWin32Error(); if (lastWin32Error == 2) { throw new FileNotFoundException("Unable to find file", fileName); } var lastError = Result.GetResultFromWin32Error(lastWin32Error); throw new IOException(string.Format(CultureInfo.InvariantCulture, "Unable to open file {0}", fileName), lastError.Code); } canRead = 0 != (access & NativeFileAccess.Read); canWrite = 0 != (access & NativeFileAccess.Write); // TODO how setup correctly canSeek flags? // Kernel32.GetFileType(SafeFileHandle handle); is not available on W8CORE canSeek = true; }
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(); }
private static extern SafeFileHandle CreateFileTransactedW( [In, MarshalAs(UnmanagedType.LPWStr)] string lpFileName, [In] NativeFileAccess dwDesiredAccess, [In] NativeFileShare dwShareMode, [In] IntPtr lpSecurityAttributes, [In] NativeFileMode dwCreationDisposition, [In] uint dwFlagsAndAttributes, [In] IntPtr hTemplateFile, [In] SafeTxHandle hTransaction, [In] IntPtr pusMiniVersion, [In] IntPtr pExtendedParameter);
internal static extern SafeFileHandle CreateFileTransactedW( [In] string lpFileName, [In] NativeFileAccess dwDesiredAccess, [In] NativeFileShare dwShareMode, [In, Optional] IntPtr lpSecurityAttributes, [In] NativeFileMode dwCreationDisposition, [In] NativeFileOptions dwFlagsAndAttributes, [In, Optional] IntPtr hTemplateFile, [In] SafeKernelTransactionHandle hTransaction, [In, Optional] IntPtr pusMiniVersion, IntPtr pExtendedParameter);
/// <summary> /// Gets the file handle to the reparse point. /// </summary> /// <param name="reparsePoint"> The reparse point. </param> /// <param name="accessMode"> The access mode. </param> /// <returns> </returns> /// <remarks> /// </remarks> private static SafeFileHandle GetReparsePointHandle(string reparsePoint, NativeFileAccess accessMode) { var reparsePointHandle = Kernel32.CreateFile(reparsePoint, accessMode, FileShare.Read | FileShare.Write | FileShare.Delete, IntPtr.Zero, FileMode.Open, NativeFileAttributesAndFlags.BackupSemantics | NativeFileAttributesAndFlags.OpenReparsePoint, IntPtr.Zero); if (Marshal.GetLastWin32Error() != 0) { throw new IOException("Unable to open reparse point.", Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error())); } return(reparsePointHandle); }
public Win32MemoryMapPager(string file, 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; Trace.WriteLine(string.Format("creating/opening file (name = {0}), access type = {1}", file, _memoryMappedFileAccess)); _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)) { long fileLengthAfterAdjustment = _fileStream.Length; if (_fileStream.Length == 0 || (_fileStream.Length % AllocationGranularity != 0)) { fileLengthAfterAdjustment = NearestSizeToAllocationGranularity(_fileInfo.Length); _fileStream.SetLength(fileLengthAfterAdjustment); } _totalAllocationSize = fileLengthAfterAdjustment; } NumberOfAllocatedPages = _totalAllocationSize / PageSize; PagerState.Release(); PagerState = CreatePagerState(); }
public static SafeFileHandle SafeCreateFile(string path, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileFlags flags, IntPtr template) { SafeFileHandle result = CreateFile(path, access, share, security, mode, flags, template); if (!result.IsInvalid && 1 != GetFileType(result)) { result.Dispose(); throw new NotSupportedException(string.Format("The specified file name '{0}' is not a disk-based file.", path)); } return(result); }
public static SafeFileHandle SafeCreateFile(string path, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileAttributesAndFlags flags, IntPtr template) { var result = Kernel32.CreateFile(path, access, share, security, mode, flags, template); if (!result.IsInvalid && FileType.Disk != Kernel32.GetFileType(result)) { result.Dispose(); throw new NotSupportedException(string.Format(Resources.Culture, Resources.Error_NonFile, path)); } return(result); }
public static SafeFileHandle SafeCreateFile(string path, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileFlags flags, IntPtr template) { SafeFileHandle result = CreateFile(path, access, share, security, mode, flags, template); if (!result.IsInvalid && 1 != GetFileType(result)) { result.Dispose(); throw new NotSupportedException(Resources.Error_NonFile(path)); } return(result); }
/// <summary> /// Creates a file stream. /// </summary> /// <param name="filename">The filename.</param> /// <param name="mode">The mode.</param> /// <param name="acccess">The access.</param> /// <returns></returns> private Stream CreateFileStream(string filename, NativeFileMode mode, NativeFileAccess acccess) { FileAccess defaultAccess = FileAccess.Read; if ((acccess & NativeFileAccess.Read) != 0 ) defaultAccess = FileAccess.Read; if ((acccess & NativeFileAccess.Write) != 0 ) defaultAccess = FileAccess.Write; if ((acccess & NativeFileAccess.ReadWrite) != 0 ) defaultAccess = FileAccess.ReadWrite; return (isTestingNativeFileStream) ? (Stream)new NativeFileStream(Name, mode, acccess) : new FileStream(filename, (FileMode)mode, defaultAccess); }
public static NativeFileAccess ToNative(this FileAccess access) { NativeFileAccess result = 0; if (FileAccess.Read == (FileAccess.Read & access)) { result |= NativeFileAccess.GenericRead; } if (FileAccess.Write == (FileAccess.Write & access)) { result |= NativeFileAccess.GenericWrite; } return(result); }
internal static SafeFileHandle SafeCreateFile(string path, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileFlags flags, IntPtr template) { var result = Kernel32.CreateFile(path, access, share, security, mode, flags, template); if (!result.IsInvalid && 1 != Kernel32.GetFileType(result)) { result.Dispose(); throw new NotSupportedException($"The specified file name '{path}' is not a disk-based file."); } return(result); }
/// <summary> /// Creates a file stream. /// </summary> /// <param name="filename">The filename.</param> /// <param name="mode">The mode.</param> /// <param name="acccess">The access.</param> /// <returns></returns> private Stream CreateFileStream(string filename, NativeFileMode mode, NativeFileAccess acccess) { FileAccess defaultAccess = FileAccess.Read; if ((acccess & NativeFileAccess.Read) != 0) { defaultAccess = FileAccess.Read; } if ((acccess & NativeFileAccess.Write) != 0) { defaultAccess = FileAccess.Write; } if ((acccess & NativeFileAccess.ReadWrite) != 0) { defaultAccess = FileAccess.ReadWrite; } return((isTestingNativeFileStream) ? (Stream) new NativeFileStream(Name, mode, acccess) : new FileStream(filename, (FileMode)mode, defaultAccess)); }
public NativeFileStream(string fileName, NativeFileMode fileMode, NativeFileAccess access, NativeFileShare share = NativeFileShare.Read) { this.handle = NativeFile.Create(fileName, access, share, IntPtr.Zero, fileMode, NativeFileOptions.None, IntPtr.Zero); if (this.handle == new IntPtr(-1)) { int lastWin32Error = NativeFileStream.MarshalGetLastWin32Error(); if (lastWin32Error == 2) throw new FileNotFoundException("Unable to find file", fileName); Result resultFromWin32Error = Result.GetResultFromWin32Error(lastWin32Error); throw new IOException(string.Format((IFormatProvider) CultureInfo.InvariantCulture, "Unable to open file {0}", new object[1] { (object) fileName }), resultFromWin32Error.Code); } else { this.canRead = (NativeFileAccess) 0 != (access & NativeFileAccess.Read); this.canWrite = (NativeFileAccess) 0 != (access & NativeFileAccess.Write); this.canSeek = true; } }
/// <summary> /// Initializes a new instance of the <see cref="NativeFileStream"/> class. /// </summary> /// <param name="fileName">Name of the file.</param> /// <param name="fileMode">The file mode.</param> /// <param name="access">The access mode.</param> /// <param name="share">The share mode.</param> public unsafe NativeFileStream(string fileName, NativeFileMode fileMode, NativeFileAccess access, NativeFileShare share = NativeFileShare.Read) { handle = NativeFile.Create(fileName, access, share, IntPtr.Zero, fileMode, NativeFileOptions.None, IntPtr.Zero); if (handle == new IntPtr(-1)) { var lastWin32Error = MarshalGetLastWin32Error(); if (lastWin32Error == 2) { throw new FileNotFoundException("Unable to find file", fileName); } var lastError = Result.GetResultFromWin32Error(lastWin32Error); throw new IOException(string.Format(CultureInfo.InvariantCulture, "Unable to open file {0}", fileName), lastError.Code); } canRead = 0 != (access & NativeFileAccess.Read); canWrite = 0 != (access & NativeFileAccess.Write); // TODO how setup correctly canSeek flags? // Kernel32.GetFileType(SafeFileHandle handle); is not available on W8CORE canSeek = true; }
public NativeFileStream(string fileName, NativeFileMode fileMode, NativeFileAccess access, NativeFileShare share = NativeFileShare.Read) { this.handle = NativeFile.Create(fileName, access, share, IntPtr.Zero, fileMode, NativeFileOptions.None, IntPtr.Zero); if (this.handle == new IntPtr(-1)) { int lastWin32Error = NativeFileStream.MarshalGetLastWin32Error(); if (lastWin32Error == 2) { throw new FileNotFoundException("Unable to find file", fileName); } Result resultFromWin32Error = Result.GetResultFromWin32Error(lastWin32Error); throw new IOException(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "Unable to open file {0}", new object[1] { (object)fileName }), resultFromWin32Error.Code); } else { this.canRead = (NativeFileAccess)0 != (access & NativeFileAccess.Read); this.canWrite = (NativeFileAccess)0 != (access & NativeFileAccess.Write); this.canSeek = true; } }
public static extern IntPtr CreateConsoleScreenBuffer(NativeFileAccess dwDesiredAccess, FileShare dwShareMode, [MarshalAs(UnmanagedType.LPStruct)] SecurityAttributes lpSecurityAttributes, Int32 dwFlags, IntPtr lpScreenBufferData);
internal static IntPtr Create(string fileName, NativeFileAccess desiredAccess, NativeFileShare shareMode, IntPtr securityAttributes, NativeFileMode mode, NativeFileOptions flagsAndOptions, IntPtr templateFile);
public static SafeFileHandle SafeCreateFile(string path, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileFlags flags, IntPtr template) { SafeFileHandle result = CreateFile(path, access, share, security, mode, flags, template); if (!result.IsInvalid && 1 != GetFileType(result)) { result.Dispose(); throw new NotSupportedException(string.Format(Resources.Culture, Resources.Error_NonFile, path)); } return result; }
/// <summary> /// Initializes a new instance of the <see cref="BitmapDecoder"/> class from a file. /// </summary> /// <param name="factory">The factory.</param> /// <param name="filename">The filename.</param> /// <param name="guidVendorRef">The GUID vendor ref.</param> /// <param name="desiredAccess">The desired access.</param> /// <param name="metadataOptions">The metadata options.</param> /// <unmanaged>HRESULT IWICImagingFactory::CreateDecoderFromFilename([In] const wchar_t* wzFilename,[In, Optional] const GUID* pguidVendor,[In] unsigned int dwDesiredAccess,[In] WICDecodeOptions metadataOptions,[Out, Fast] IWICBitmapDecoder** ppIDecoder)</unmanaged> public BitmapDecoder(ImagingFactory factory, string filename, System.Guid? guidVendorRef, NativeFileAccess desiredAccess, SharpDX.WIC.DecodeOptions metadataOptions) { factory.CreateDecoderFromFilename(filename, guidVendorRef, (int)desiredAccess, metadataOptions, this); }
/// <summary> /// Initializes a new instance of the <see cref="BitmapDecoder"/> class from a file. /// </summary> /// <param name="factory">The factory.</param> /// <param name="filename">The filename.</param> /// <param name="desiredAccess">The desired access.</param> /// <param name="metadataOptions">The metadata options.</param> /// <unmanaged>HRESULT IWICImagingFactory::CreateDecoderFromFilename([In] const wchar_t* wzFilename,[In, Optional] const GUID* pguidVendor,[In] unsigned int dwDesiredAccess,[In] WICDecodeOptions metadataOptions,[Out, Fast] IWICBitmapDecoder** ppIDecoder)</unmanaged> public BitmapDecoder(ImagingFactory factory, string filename, NativeFileAccess desiredAccess, SharpDX.WIC.DecodeOptions metadataOptions) : this(factory, filename, null, desiredAccess, metadataOptions) { }
public static extern SafeFileHandle CreateFile(string lpFileName, NativeFileAccess dwDesiredAccess, NativeFileShare dwShareMode, IntPtr lpSecurityAttributes, NativeFileCreationDisposition dwCreationDisposition, NativeFileAttributes dwFlagsAndAttributes, IntPtr hTemplateFile);
/// <summary> /// Initializes a new instance of the <see cref="WICStream"/> class from a file. /// </summary> /// <param name="factory">The factory.</param> /// <param name="fileName">Name of the file.</param> /// <param name="fileAccess">The file access.</param> /// <msdn-id>ee690325</msdn-id> /// <unmanaged>HRESULT IWICImagingFactory::CreateStream([Out, Fast] IWICStream** ppIWICStream)</unmanaged> public WICStream(ImagingFactory factory, string fileName, NativeFileAccess fileAccess) : base(IntPtr.Zero) { factory.CreateStream(this); InitializeFromFilename(fileName, (int)fileAccess); }
public IWICBitmapDecoder CreateDecoderFromFileName(string fileName, Guid?guidVendor, FileAccess desiredAccess = FileAccess.Read, DecodeOptions metadataOptions = DecodeOptions.CacheOnDemand) { NativeFileAccess nativeAccess = desiredAccess.ToNative(); return(CreateDecoderFromFilename_(fileName, guidVendor, (int)nativeAccess, metadataOptions)); }
internal static extern SafeFileHandle CreateFile(string fileName, NativeFileAccess fileAccess, NativeFileShare fileShare, IntPtr securityAttributes, NativeFileMode creationDisposition, NativeFileFlag flags, IntPtr template);
public static extern SafeFileHandle CreateFile(string name, NativeFileAccess access, FileShare share, IntPtr security, FileMode mode, NativeFileAttributesAndFlags flags, IntPtr template);
internal static extern IntPtr Create( string fileName, NativeFileAccess desiredAccess, NativeFileShare shareMode, NativeFileMode mode, IntPtr extendedParameters);
/// <summary> /// Initializes a new instance of the <see cref="BitmapDecoder"/> class from a file. /// </summary> /// <param name="factory">The factory.</param> /// <param name="filename">The filename.</param> /// <param name="guidVendorRef">The GUID vendor ref.</param> /// <param name="desiredAccess">The desired access.</param> /// <param name="metadataOptions">The metadata options.</param> /// <unmanaged>HRESULT IWICImagingFactory::CreateDecoderFromFilename([In] const wchar_t* wzFilename,[In, Optional] const GUID* pguidVendor,[In] unsigned int dwDesiredAccess,[In] WICDecodeOptions metadataOptions,[Out, Fast] IWICBitmapDecoder** ppIDecoder)</unmanaged> public BitmapDecoder(ImagingFactory factory, string filename, System.Guid?guidVendorRef, NativeFileAccess desiredAccess, SharpDX.WIC.DecodeOptions metadataOptions) { factory.CreateDecoderFromFilename(filename, guidVendorRef, (int)desiredAccess, metadataOptions, this); }
private static SafeFileHandle GetReparsePointHandle(string reparsePoint, NativeFileAccess accessMode) { var reparsePointHandle = Kernel32.CreateFile(reparsePoint, accessMode, FileShare.Read | FileShare.Write | FileShare.Delete, IntPtr.Zero, FileMode.Open, NativeFileAttributesAndFlags.BackupSemantics | NativeFileAttributesAndFlags.OpenReparsePoint, IntPtr.Zero); if (Marshal.GetLastWin32Error() != 0) { throw new IOException("Unable to open reparse point.", Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error())); } return reparsePointHandle; }