private Stream OpenFileStream() { FileAccess fileAccess = IsReadOnly ? FileAccess.Read : FileAccess.ReadWrite; // We should use noncached I/O operations to avoid excessive RAM usage. // Note: KB99794 provides information about FILE_FLAG_WRITE_THROUGH and FILE_FLAG_NO_BUFFERING. // We must avoid using buffered writes, using it will negatively affect the performance and reliability. // Note: once the file system write buffer is filled, Windows may delay any (buffer-dependent) pending write operations, which will create a deadlock. if (IsWin32()) { uint flags = unchecked ((uint)(FILE_FLAG_NO_BUFFERING | FileOptions.WriteThrough)); if (m_useOverlappedIO) { flags |= (uint)FILE_FLAG_OVERLAPPED; } Microsoft.Win32.SafeHandles.SafeFileHandle handle = HandleUtils.GetFileHandle(this.Path, fileAccess, ShareMode.Read, flags); if (!handle.IsInvalid) { return(new FileStreamEx(handle, fileAccess)); } else { // we always release invalid handle int errorCode = System.Runtime.InteropServices.Marshal.GetLastWin32Error(); string message = String.Format("Failed to obtain file handle for file '{0}'.", this.Path); IOExceptionHelper.ThrowIOError(errorCode, message); return(null); // this line will not be reached } } else { return(new FileStream(this.Path, FileMode.Open, fileAccess, FileShare.Read, m_bytesPerSector, FILE_FLAG_NO_BUFFERING | FileOptions.WriteThrough)); } }
/// <param name="newAllocation">True if a new handle has been allocated for the caller, the caller must release the handle eventually</param> public static SafeFileHandle ObtainHandle(Guid volumeGuid, FileAccess access, ShareMode shareMode, out bool newAllocation) { if (m_handlePool.ContainsKey(volumeGuid)) { newAllocation = false; return(m_handlePool[volumeGuid]); } else { newAllocation = true; SafeFileHandle handle = HandleUtils.GetVolumeHandle(volumeGuid, access, shareMode); m_handlePool.Add(volumeGuid, handle); return(handle); } }
/// <param name="newAllocation">True if a new handle has been allocated for the caller, the caller must release the handle eventually</param> public static SafeFileHandle ObtainHandle(int physicalDiskIndex, FileAccess access, ShareMode shareMode, out bool newAllocation) { if (m_handlePool.ContainsKey(physicalDiskIndex)) { newAllocation = false; return(m_handlePool[physicalDiskIndex]); } else { newAllocation = true; SafeFileHandle handle = HandleUtils.GetDiskHandle(physicalDiskIndex, access, shareMode); m_handlePool.Add(physicalDiskIndex, handle); return(handle); } }
public static List <int> GetPhysicalDiskIndexList() { List <string> devicePathList = DeviceInterfaceUtils.GetDevicePathList(DeviceInterfaceUtils.DiskClassGuid); List <int> result = new List <int>(); foreach (string devicePath in devicePathList) { SafeFileHandle hDevice = HandleUtils.GetFileHandle(devicePath, FileAccess.Read, ShareMode.ReadWrite); STORAGE_DEVICE_NUMBER number = GetDeviceNumber(hDevice); hDevice.Close(); result.Add((int)number.DeviceNumber); } // We'll now sort the list based on disk number result.Sort(); return(result); }
/// <summary> /// Note: The NTFS file system treats a locked volume as a dismounted volume. /// </summary> public static bool IsVolumeMounted(Guid volumeGuid) { SafeFileHandle handle = HandleUtils.GetVolumeHandle(volumeGuid, FileAccess.Read, ShareMode.ReadWrite); return(IsVolumeMounted(handle)); }
/// <summary> /// Note: The NTFS file system treats a locked volume as a dismounted volume. /// </summary> public static bool IsVolumeMounted(string path) { SafeFileHandle handle = HandleUtils.GetVolumeHandle(path, FileAccess.Read, ShareMode.ReadWrite); return(IsVolumeMounted(handle)); }
/// <summary> /// Note: The NTFS file system treats a locked volume as a dismounted volume. /// </summary> public static bool IsVolumeMounted(char driveLetter) { SafeFileHandle handle = HandleUtils.GetVolumeHandle(driveLetter, FileAccess.Read, ShareMode.ReadWrite); return(IsVolumeMounted(handle)); }