Example #1
0
        private void DeleteConflictingDestinationFolderWithoutDeletingThisFolder(string fullDestinationPath)
        {
            // Both Move and Copy require manual deletion of a conflicting folder at the destination
            // with the ReplaceExisting option.
            // This can be a problem if we want to move/copy a folder to the same location,
            // because we'd delete the destination folder which is *also the source folder*.
            // In essence, the folder would be gone for good.
            // We prevent this with a little hack: Temporarily renaming the source folder
            // so that deleting the destination does not automatically delete the source.
            // After the deletion, we undo the rename and can continue with the actual moving/copying.
            var tmpFolderName = PhysicalPathHelper.GetTemporaryElementName();
            var tmpFolderPath = IOPath.Combine(_fullParentPath?.ToString() ?? "", tmpFolderName);

            Directory.Move(_fullPath.ToString(), tmpFolderPath);

            try
            {
                Directory.Delete(fullDestinationPath, recursive: true);
            }
            catch (DirectoryNotFoundException)
            {
            }
            finally
            {
                Directory.Move(tmpFolderPath, _fullPath.ToString());
            }
        }
Example #2
0
        internal PhysicalStoragePath(string path, FileSystem fileSystem)
            : base(fileSystem, path)
        {
            Debug.Assert(
                ReferenceEquals(fileSystem.PathInformation, PhysicalPathHelper.PhysicalPathInformation),
                $"When using the PhysicalStoragePath, your file system should be using the corresponding " +
                $"{nameof(PhysicalPathHelper.PhysicalPathInformation)}."
                );

            var fullPath = GetFullPathOrThrow(path);
            var rootPath = Path.GetPathRoot(path);
            var pathWithoutTrailingSeparator = PathPolyfills.TrimEndingDirectorySeparator(path);
            var directoryPath        = Path.GetDirectoryName(pathWithoutTrailingSeparator);
            var name                 = Path.GetFileName(pathWithoutTrailingSeparator);
            var nameWithoutExtension = GetNameWithoutExtension(name);
            var extension            = PhysicalPathHelper.GetExtensionWithoutTrailingExtensionSeparator(pathWithoutTrailingSeparator);
            var isPathFullyQualified = PathPolyfills.IsPathFullyQualified(path);

            Kind = isPathFullyQualified ? PathKind.Absolute : PathKind.Relative;
            Name = name;
            NameWithoutExtension = nameWithoutExtension;
            Extension            = string.IsNullOrEmpty(extension) ? null : extension;

            _rootLazy = new Lazy <StoragePath?>(
                () => string.IsNullOrEmpty(rootPath) ? null : fileSystem.GetPath(rootPath)
                );

            _parentLazy = new Lazy <StoragePath?>(
                () => string.IsNullOrEmpty(directoryPath) ? null : fileSystem.GetPath(directoryPath)
                );

            _fullPathLazy = new Lazy <StoragePath>(
                () => fileSystem.GetPath(fullPath)
                );
Example #3
0
        public override async Task <StorageFolderProperties> GetPropertiesAsync(CancellationToken cancellationToken = default)
        {
            var folder = await FsHelper.GetFolderAsync(_fullPath, cancellationToken).ConfigureAwait(false);

            var props = await folder.GetBasicPropertiesAsync().AsAwaitable(cancellationToken);

            var lastModified = props.DateModified == default ? (DateTimeOffset?)null : props.DateModified;

            return(new StorageFolderProperties(
                       folder.Name,
                       IOPath.GetFileNameWithoutExtension(folder.Name),
                       PhysicalPathHelper.GetExtensionWithoutTrailingExtensionSeparator(folder.Name)?.ToNullIfEmpty(),
                       folder.DateCreated,
                       lastModified
                       ));
        }
Example #4
0
        public override Task <StorageFolderProperties> GetPropertiesAsync(CancellationToken cancellationToken = default)
        {
            return(Task.Run(() =>
            {
                EnsureExists();

                // Attempting to get the real folder name can fail, e.g. the folder might have been deleted in between.
                // In such a case, simply return the last fetched name. It will happen rarely and is good enough
                // for such cases.
                cancellationToken.ThrowIfCancellationRequested();
                var realFolderName = FsHelper.GetRealDirectoryName(_fullPath.ToString()) ?? Path.Name;
                var lastWriteTime = Directory.GetLastWriteTimeUtc(_fullPath.ToString());

                return new StorageFolderProperties(
                    realFolderName,
                    IOPath.GetFileNameWithoutExtension(realFolderName),
                    PhysicalPathHelper.GetExtensionWithoutTrailingExtensionSeparator(realFolderName)?.ToNullIfEmpty(),
                    Directory.GetCreationTimeUtc(_fullPath.ToString()),
                    lastWriteTime
                    );
            }, cancellationToken));
        }