/// <summary> /// Moves file part using virtual cluster, logical cluster on disk and amount of clusters /// </summary> /// <param name="path">FileName</param> /// <param name="vcn">Virtual cluster</param> /// <param name="lcn">Cluster on disk</param> /// <param name="count">Amount of clusters</param> public void MoveFile(string path, Int64 vcn, Int64 lcn, Int32 count) { string deviceName = path.Substring(0, 2); IntPtr hVol = IntPtr.Zero; IntPtr hFile = IntPtr.Zero; try { hVol = OpenVolume(deviceName); hFile = OpenFile(path); MoveFileData mfd = new MoveFileData { hFile = hFile, StartingVcn = vcn, StartingLcn = lcn, ClusterCount = count }; GCHandle handle = GCHandle.Alloc(mfd, GCHandleType.Pinned); IntPtr p = handle.AddrOfPinnedObject(); uint bufSize = (uint)Marshal.SizeOf(mfd); uint size = 0; bool fResult = DeviceIoControl( hVol, FsConst.FsctlMoveFile, p, bufSize, IntPtr.Zero, // no output data from this FSCTL 0, ref size, IntPtr.Zero); handle.Free(); if (!fResult) { throw new DefragAccessDeniedException(Marshal.GetLastWin32Error().ToString(CultureInfo.InvariantCulture)); } } finally { CloseHandle(hVol); CloseHandle(hFile); } }
/// <summary> /// Moves file part using virtual cluster, logical cluster on disk and amount of clusters /// </summary> /// <param name="path">FileName</param> /// <param name="vcn">Virtual cluster</param> /// <param name="lcn">Cluster on disk</param> /// <param name="count">Amount of clusters</param> public void MoveFile(string path, Int64 vcn, Int64 lcn, Int32 count) { string deviceName = path.Substring(0, 2); IntPtr hVol = IntPtr.Zero; IntPtr hFile = IntPtr.Zero; try { hVol = OpenVolume(deviceName); hFile = OpenFile(path); MoveFileData mfd = new MoveFileData { hFile = hFile, StartingVcn = vcn, StartingLcn = lcn, ClusterCount = count }; GCHandle handle = GCHandle.Alloc(mfd, GCHandleType.Pinned); IntPtr p = handle.AddrOfPinnedObject(); uint bufSize = (uint) Marshal.SizeOf(mfd); uint size = 0; bool fResult = DeviceIoControl( hVol, FsConst.FsctlMoveFile, p, bufSize, IntPtr.Zero, // no output data from this FSCTL 0, ref size, IntPtr.Zero); handle.Free(); if (!fResult) { throw new DefragAccessDeniedException(Marshal.GetLastWin32Error().ToString(CultureInfo.InvariantCulture)); } } finally { CloseHandle(hVol); CloseHandle(hFile); } }