Ejemplo n.º 1
0
        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));
            }
        }
Ejemplo n.º 2
0
        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);
        }