/// <summary> /// Retrieves the drive names of all logical drives on a computer. /// </summary> /// <returns>An array of type <see cref="DriveInfoBase"/> that represents the logical drives on a computer.</returns> public DriveInfoBase[] GetDrives() { var driveInfos = AfsDriveInfo.GetDrives(); var driveInfoWrappers = new DriveInfoBase[driveInfos.Length]; for (int index = 0; index < driveInfos.Length; index++) { var driveInfo = driveInfos[index]; driveInfoWrappers[index] = new DriveInfoWrapper(fileSystem, driveInfo); } return(driveInfoWrappers); }
protected virtual void ExecuteCopy(Alphaleonis.Win32.Filesystem.DriveInfo driverInfo, FileDataInfo file) { if (driverInfo.TotalFreeSpace < file.Size) { ReleaseResources(); throw new NotEnoughSpaceException(string.Format("{0} ({1})", driverInfo.VolumeLabel, driverInfo.Name)); } Writer.SetLength(Reader.Length); readBytes = 0; FileBytesTransferred = 0; executeCopyException = null; try { while ((readBytes = Reader.Read(buffer, 0, BufferSize)) > 0) { Writer.Write(buffer, 0, readBytes); //Status FileBytesTransferred += readBytes; TotalBytesTransferred += readBytes; } } catch (Exception ex) { executeCopyException = ex; } finally { ReleaseResources(); CurrentFile = null; if (executeCopyException != null) { throw executeCopyException; } } }
private void AlphaFS_Volume_DefineDosDevice_RegularDriveMapping(bool isNetwork) { using (var tempRoot = new TemporaryDirectory(isNetwork)) { var folder = tempRoot.CreateDirectory(); var drive = Alphaleonis.Win32.Filesystem.DriveInfo.GetFreeDriveLetter() + ":"; Assert.IsFalse(System.IO.Directory.Exists(drive), "The drive exists, but it is expected not to."); try { Alphaleonis.Win32.Filesystem.Volume.DefineDosDevice(drive, folder.FullName); Assert.IsTrue(System.IO.Directory.Exists(drive), "The drive does not exists, but it is expected to."); var dirInfoSysIO = new System.IO.DriveInfo(drive); var dirInfoAlphaFS = new Alphaleonis.Win32.Filesystem.DriveInfo(drive); UnitTestConstants.Dump(dirInfoSysIO, -21); UnitTestConstants.Dump(dirInfoAlphaFS, -21); Assert.AreEqual(dirInfoSysIO.Name, dirInfoAlphaFS.Name); Assert.AreEqual(dirInfoSysIO.DriveType, dirInfoAlphaFS.DriveType); Assert.AreEqual(dirInfoSysIO.TotalSize, dirInfoAlphaFS.TotalSize); } finally { Alphaleonis.Win32.Filesystem.Volume.DeleteDosDevice(drive); Assert.IsFalse(System.IO.Directory.Exists(drive), "The drive exists, but it is expected not to."); } } Console.WriteLine(); }
/// <summary> /// Execute copy without set the file length before copy. /// </summary> /// <param name="driverInfo"></param> /// <param name="file"></param> protected virtual void ExecuteCopySafe(Alphaleonis.Win32.Filesystem.DriveInfo driverInfo, FileDataInfo file) { if (driverInfo.TotalFreeSpace < file.Size) { ReleaseResources(); throw new NotEnoughSpaceException(string.Format("{0} ({1})", driverInfo.VolumeLabel, driverInfo.Name)); } //Writer.SetLength(Reader.Length); readBytes = 0; FileBytesTransferred = 0; while ((readBytes = Reader.Read(buffer, 0, BufferSize)) > 0) { // Request the lock, and block until it is obtained. Monitor.Enter(Writer); //If operation was cancelled, them the writer has been released //and this statement produce: The Thread has been aborted exception. //Them capture the exception and terminate the current file copy process. //try //{ Writer.Write(buffer, 0, readBytes); //Status FileBytesTransferred += readBytes; TotalBytesTransferred += readBytes; //} //catch (Exception ex) //{ // System.Windows.MessageBox.Show(ex.Message + " in FileCopier.Copy"); //} // Ensure that the lock is released. Monitor.Exit(Writer); } CurrentFile = null; }
public override void CopyFile(FileDataInfo file) { CurrentFile = file; fileName = Delimon.Win32.IO.Path.GetPathRoot(file.DestinyPath); driverInfo = new Alphaleonis.Win32.Filesystem.DriveInfo(fileName); using (Reader = file.GetStreamToRead()) { using (Writer = file.GetStreamToWrite(FileMode.Create)) { //if (driverInfo.DriveType == DriveType.Removable && driverInfo.DriveFormat=="FAT32") //{ // ExecuteCopySafe(driverInfo, file); //} //else ExecuteCopy(driverInfo, file); ExecuteCopy(driverInfo, file); } //} } }
private void DriveInfo_InitializeInstance(bool isNetwork) { UnitTestConstants.PrintUnitTestHeader(isNetwork); Console.WriteLine(); var drive = UnitTestConstants.SysDrive[0].ToString(); if (isNetwork) { // Only using a drive letter results in a wrong UNC path. drive = Alphaleonis.Win32.Filesystem.Path.LocalToUnc(UnitTestConstants.SysDrive); } Console.WriteLine("Input Drive Path: [{0}]", drive); var actual = new Alphaleonis.Win32.Filesystem.DriveInfo(drive); Assert.IsTrue(actual.IsReady); Assert.IsTrue(actual.IsVolume); if (isNetwork) { Assert.IsTrue(actual.IsUnc); } else { Assert.IsFalse(actual.IsUnc); } // System.IO.DriveInfo cannot handle UNC paths. if (!isNetwork) { // Even 1 byte more or less results in failure, so do these tests asap. var expected = new System.IO.DriveInfo(drive); Assert.AreEqual(expected.AvailableFreeSpace, actual.AvailableFreeSpace, "AvailableFreeSpace AlphaFS != System.IO"); Assert.AreEqual(expected.TotalFreeSpace, actual.TotalFreeSpace, "TotalFreeSpace AlphaFS != System.IO"); Assert.AreEqual(expected.TotalSize, actual.TotalSize, "TotalSize AlphaFS != System.IO"); Assert.AreEqual(expected.DriveFormat, actual.DriveFormat, "DriveFormat AlphaFS != System.IO"); Assert.AreEqual(expected.DriveType, actual.DriveType, "DriveType AlphaFS != System.IO"); Assert.AreEqual(expected.IsReady, actual.IsReady, "IsReady AlphaFS != System.IO"); Assert.AreEqual(expected.Name, actual.Name, "Name AlphaFS != System.IO"); Assert.AreEqual(expected.RootDirectory.ToString(), actual.RootDirectory.ToString(), "RootDirectory AlphaFS != System.IO"); Assert.AreEqual(expected.VolumeLabel, actual.VolumeLabel, "VolumeLabel AlphaFS != System.IO"); UnitTestConstants.Dump(expected, -18); Console.WriteLine(); } UnitTestConstants.Dump(actual, -21); UnitTestConstants.Dump(actual.DiskSpaceInfo, -26); UnitTestConstants.Dump(actual.VolumeInfo, -26); Console.WriteLine(); }
private void AlphaFS_Volume_GetVolumeInfo_FromLogicalDrive(bool isNetwork) { UnitTestConstants.PrintUnitTestHeader(isNetwork); var logicalDriveCount = 0; foreach (var logicalDrive in System.IO.DriveInfo.GetDrives()) { var driveName = isNetwork ? Alphaleonis.Win32.Filesystem.Path.LocalToUnc(logicalDrive.Name) : logicalDrive.Name; Console.WriteLine("#{0:000}\tInput Logical Drive Path: [{1}]", ++logicalDriveCount, driveName); // Skip mapped drives and CDRom drives. if (logicalDrive.DriveType == System.IO.DriveType.NoRootDirectory || logicalDrive.DriveType == System.IO.DriveType.CDRom) { Console.WriteLine(); continue; } if (isNetwork) { var driveInfo2 = new Alphaleonis.Win32.Filesystem.DriveInfo(driveName); // GetVolumeInfo fails when DriveType is not of type Network. if (driveInfo2.DriveType != System.IO.DriveType.Network) { continue; } var volInfo = Alphaleonis.Win32.Filesystem.Volume.GetVolumeInfo(driveName); UnitTestConstants.Dump(volInfo); Assert.AreEqual(driveInfo2.VolumeLabel, volInfo.Name); Assert.AreEqual(driveInfo2.DriveFormat, volInfo.FileSystemName); if (logicalDrive.DriveType != System.IO.DriveType.Network) { Assert.AreEqual(driveInfo2.Name, volInfo.FullPath); } Assert.IsNull(volInfo.Guid); } else { Alphaleonis.Win32.Filesystem.VolumeInfo volInfo; try { volInfo = Alphaleonis.Win32.Filesystem.Volume.GetVolumeInfo(driveName); } catch (Exception ex) { Console.WriteLine("\nCaught (UNEXPECTED) {0}: [{1}]\n", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, " ")); continue; } UnitTestConstants.Dump(volInfo); // System.IO.DriveInfo does not support UNC paths. var driveInfo2 = new System.IO.DriveInfo(driveName); Assert.AreEqual(driveInfo2.VolumeLabel, volInfo.Name); Assert.AreEqual(driveInfo2.DriveFormat, volInfo.FileSystemName); Assert.AreEqual(driveInfo2.Name, volInfo.FullPath); if (logicalDrive.DriveType != System.IO.DriveType.Network) { Assert.IsNotNull(volInfo.Guid); Assert.IsTrue(volInfo.Guid.StartsWith(Alphaleonis.Win32.Filesystem.Path.VolumePrefix)); } } Console.WriteLine(); } Assert.IsTrue(logicalDriveCount > 0, "No logical drives enumerated, but it is expected."); Console.WriteLine(); }
private void Volume_GetVolumeInfo_FromLogicalDrive(bool isNetwork) { UnitTestConstants.PrintUnitTestHeader(isNetwork); var logicalDriveCount = 0; foreach (var logicalDrive in System.IO.DriveInfo.GetDrives()) { var driveName = isNetwork ? Alphaleonis.Win32.Filesystem.Path.LocalToUnc(logicalDrive.Name) : logicalDrive.Name; Console.WriteLine("\n#{0:000}\tInput Logical Drive Path: [{1}]", logicalDriveCount, driveName); if (logicalDrive.DriveType == System.IO.DriveType.CDRom) { Console.WriteLine(); continue; } if (isNetwork) { var driveInfo2 = new Alphaleonis.Win32.Filesystem.DriveInfo(driveName); // GetVolumeInfo fails when DriveType is not of type Network. if (driveInfo2.DriveType != System.IO.DriveType.Network) { continue; } var volInfo = Alphaleonis.Win32.Filesystem.Volume.GetVolumeInfo(driveName); UnitTestConstants.Dump(volInfo, -26); Assert.AreEqual(driveInfo2.VolumeLabel, volInfo.Name); Assert.AreEqual(driveInfo2.DriveFormat, volInfo.FileSystemName); Assert.AreEqual(driveInfo2.Name, volInfo.FullPath); Assert.IsNull(volInfo.Guid); logicalDriveCount++; } else { var volInfo = Alphaleonis.Win32.Filesystem.Volume.GetVolumeInfo(driveName); UnitTestConstants.Dump(volInfo, -26); // System.IO.DriveInfo does not support UNC paths. var driveInfo2 = new System.IO.DriveInfo(driveName); Assert.AreEqual(driveInfo2.VolumeLabel, volInfo.Name); Assert.AreEqual(driveInfo2.DriveFormat, volInfo.FileSystemName); Assert.AreEqual(driveInfo2.Name, volInfo.FullPath); Assert.IsNotNull(volInfo.Guid); Assert.IsTrue(volInfo.Guid.StartsWith(Alphaleonis.Win32.Filesystem.Path.VolumePrefix)); logicalDriveCount++; } Console.WriteLine(); } Assert.IsTrue(logicalDriveCount > 0, "No logical drives enumerated, but it is expected."); Console.WriteLine(); }
public DriveInfoBase FromDriveName(string driveName) { var realDriveInfo = new AfsDriveInfo(driveName); return(new DriveInfoWrapper(fileSystem, realDriveInfo)); }
public void AlphaFS_Volume_DefineDosDevice_Local_Success() { UnitTestConstants.PrintUnitTestHeader(false); Console.WriteLine(); if (!UnitTestConstants.IsAdmin()) { Assert.Inconclusive(); } var TempFolder = UnitTestConstants.TempFolder; #region Regular Drive Mapping var drive = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", Alphaleonis.Win32.Filesystem.DriveInfo.GetFreeDriveLetter(), Alphaleonis.Win32.Filesystem.Path.VolumeSeparatorChar, Alphaleonis.Win32.Filesystem.Path.DirectorySeparatorChar); // Create Regular drive mapping. var actionIsTrue = false; try { Alphaleonis.Win32.Filesystem.Volume.DefineDosDevice(drive, TempFolder); actionIsTrue = true; } catch { } Console.WriteLine("Created Regular drive mapping (Should be True): [{0}]\nDrive letter: [{1}] now points to: [{2}]", actionIsTrue, drive, TempFolder); Assert.IsTrue(actionIsTrue, "Regular drive mapping should have been created."); var di = new Alphaleonis.Win32.Filesystem.DriveInfo(drive); var diSysIo = new System.IO.DriveInfo(drive); try { Assert.IsTrue(UnitTestConstants.Dump(di, -21)); Assert.AreEqual(diSysIo.IsReady, di.IsReady); } finally { // Remove Regular drive mapping. actionIsTrue = false; try { Alphaleonis.Win32.Filesystem.Volume.DeleteDosDevice(drive); actionIsTrue = true; } catch { } Console.WriteLine(); Console.WriteLine("Removed Regular drive mapping (Should be True): [{0}]\nDrive letter: [{1}] has been set free.", actionIsTrue, drive); Console.WriteLine(); Assert.IsTrue(actionIsTrue, "Regular drive mapping should have been removed."); Assert.IsFalse(System.IO.Directory.Exists(drive), "Drive letter should not be visible."); } #endregion // Regular Drive Mapping #region Symbolic Link Drive Mapping try { drive = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", Alphaleonis.Win32.Filesystem.DriveInfo.GetFreeDriveLetter(true), Alphaleonis.Win32.Filesystem.Path.VolumeSeparatorChar, Alphaleonis.Win32.Filesystem.Path.DirectorySeparatorChar); UnitTestConstants.StopWatcher(true); // Create Symbolic Link. var createSymbolicLink = false; try { Alphaleonis.Win32.Filesystem.Volume.DefineDosDevice(drive, TempFolder, Alphaleonis.Win32.Filesystem.DosDeviceAttributes.RawTargetPath); createSymbolicLink = true; } catch { } Console.WriteLine("\n\nCreated Symbolic link mapping (Should be True): [{0}]\nDrive letter: [{1}] now points to: [{2}]", createSymbolicLink, drive, TempFolder); Assert.IsTrue(createSymbolicLink); di = new Alphaleonis.Win32.Filesystem.DriveInfo(drive); Assert.IsTrue(UnitTestConstants.Dump(di, -21)); // The link is created in the NT Device Name namespace and thus not visibile in Explorer. // Remove Symbolic Link, no exact match: fail. var removeSymbolicLink = false; try { Alphaleonis.Win32.Filesystem.Volume.DeleteDosDevice(drive, "NonExistingFolder", true); removeSymbolicLink = true; } catch { } Console.WriteLine("\n\nRemoved Symbolic link mapping (Should be False): [{0}]\nDrive letter: [{1}] has NOT been set free.\tNo exactMatch MS-DOS device name found.", removeSymbolicLink, drive); Assert.IsFalse(removeSymbolicLink); } finally { // Remove Symbolic Link, exact match: success. var removeSymbolicLink = false; try { Alphaleonis.Win32.Filesystem.Volume.DeleteDosDevice(drive, TempFolder, true); removeSymbolicLink = true; } catch { } Console.WriteLine("\n\nRemoved Symbolic link mapping (Should be True): [{0}]\nDrive letter: [{1}] has been set free.\tFound exactMatch MS-DOS device name.", removeSymbolicLink, drive); Assert.IsTrue(removeSymbolicLink); Assert.IsFalse(System.IO.Directory.Exists(drive)); } #endregion // Symbolic Link Drive Mapping }
/// <summary> /// Initializes a new instance of the <see cref="DriveInfoWrapper"/> class, which acts as a wrapper for a drive info. /// </summary> /// <param name="fileSystem"></param> /// <param name="instance">The drive info.</param> public DriveInfoWrapper(IFileSystem fileSystem, AfsDriveInfo instance) : base(fileSystem) { _instance = instance ?? throw new ArgumentNullException(nameof(instance)); }
protected override void ExecuteCopy(Alphaleonis.Win32.Filesystem.DriveInfo driverInfo, FileDataInfo file) { CurrentFile = null; }
protected override void ExecuteCopy(Alphaleonis.Win32.Filesystem.DriveInfo driverInfo, FileDataInfo file) { buffer = new byte[CalculateBufferSize(file.Size)]; base.ExecuteCopy(driverInfo, file); }