Пример #1
0
        /// <summary>
        /// CheckTrim Checks for trim descriptor
        /// </summary>
        /// <param name="DiskIndex">Physical disk index</param>
        /// <returns>
        /// true if trim is enabled else false.
        /// </returns>
        private static bool CheckTrim(int DiskIndex)
        {
            DEVICE_TRIM_DESCRIPTOR TrimDesc     = new DEVICE_TRIM_DESCRIPTOR();
            STORAGE_PROPERTY_QUERY StorageQuery = new STORAGE_PROPERTY_QUERY();
            uint BytesReturned = 0;

            StorageQuery.PropertyId = STORAGE_PROPERTY_ID.StorageDeviceTrimProperty;
            StorageQuery.QueryType  = STORAGE_QUERY_TYPE.PropertyStandardQuery;

            IntPtr StorageQueryPtr = Marshal.AllocHGlobal(Marshal.SizeOf(StorageQuery));
            IntPtr TrimDescPtr     = Marshal.AllocHGlobal(Marshal.SizeOf(TrimDesc));

            Marshal.StructureToPtr(StorageQuery, StorageQueryPtr, false);
            Marshal.StructureToPtr(TrimDesc, TrimDescPtr, false);

            SafeFileHandle physicalDisk = DeviceIO.CreateFile(
                "\\\\.\\PhysicalDrive" + DiskIndex,
                GENERIC_READ,
                FILE_SHARE_READ,
                IntPtr.Zero,
                OPEN_EXISTING,
                FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH,
                IntPtr.Zero);

            bool rs = DeviceIoControl(physicalDisk, IOCTL_STORAGE_QUERY_PROPERTY, StorageQueryPtr,
                                      (uint)Marshal.SizeOf(StorageQuery), TrimDescPtr, (uint)Marshal.SizeOf(TrimDesc),
                                      ref BytesReturned, IntPtr.Zero);

            if (rs == false)
            {
                throw new Exception();
            }
            TrimDesc = (DEVICE_TRIM_DESCRIPTOR)Marshal.PtrToStructure(TrimDescPtr, typeof(DEVICE_TRIM_DESCRIPTOR));
            return(TrimDesc.TrimEnabled);
        }
Пример #2
0
        /// <summary>
        /// Opens file handle for the required drive.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="desiredAccess"></param>
        /// <returns>
        /// Pointer to the drive
        /// </returns>
        private SafeFileHandle OpenFile(string id, FileAccess desiredAccess)
        {
            uint access;

            switch (desiredAccess)
            {
            case FileAccess.Read:
                access = DeviceIO.GENERIC_READ;
                break;

            case FileAccess.Write:
                access = DeviceIO.GENERIC_WRITE;
                break;

            case FileAccess.ReadWrite:
                access = DeviceIO.GENERIC_READ | DeviceIO.GENERIC_WRITE;
                break;

            default:
                access = DeviceIO.GENERIC_READ;
                break;
            }

            SafeFileHandle ptr = DeviceIO.CreateFile(
                id,
                access,
                DeviceIO.FILE_SHARE_READ,
                IntPtr.Zero,
                DeviceIO.OPEN_EXISTING,
                DeviceIO.FILE_FLAG_NO_BUFFERING | DeviceIO.FILE_FLAG_WRITE_THROUGH,
                IntPtr.Zero);

            if (ptr.IsInvalid)
            {
                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
            }

            return(ptr);
        }