Exemplo n.º 1
0
        public async Task <IStorageHistory> RenameAsync(PathWithType source,
                                                        string newName,
                                                        NameCollisionOption collision,
                                                        IProgress <FilesystemErrorCode> errorCode,
                                                        CancellationToken cancellationToken)
        {
            if (Path.GetFileName(source.Path) == newName && collision == NameCollisionOption.FailIfExists)
            {
                errorCode?.Report(FilesystemErrorCode.ERROR_ALREADYEXIST);
                return(null);
            }

            if (!string.IsNullOrWhiteSpace(newName) &&
                !FilesystemHelpers.ContainsRestrictedCharacters(newName) &&
                !FilesystemHelpers.ContainsRestrictedFileName(newName))
            {
                try
                {
                    IStorageItem itemToRename = await source.Path.ToStorageItem();

                    await itemToRename.RenameAsync(newName, collision);

                    errorCode?.Report(FilesystemErrorCode.ERROR_SUCCESS);
                    return(new StorageHistory(FileOperationType.Rename, source, new PathWithType(itemToRename.Path, source.ItemType)));
                }
                catch (Exception e)
                {
                    errorCode?.Report(FilesystemTasks.GetErrorCode(e));
                    return(null);
                }
            }

            return(null);
        }
Exemplo n.º 2
0
        public async Task <IStorageHistory> RenameAsync(IStorageItem source,
                                                        string newName,
                                                        NameCollisionOption collision,
                                                        IProgress <FilesystemErrorCode> errorCode,
                                                        CancellationToken cancellationToken)
        {
            if (Path.GetFileName(source.Path) == newName && collision == NameCollisionOption.FailIfExists)
            {
                errorCode?.Report(FilesystemErrorCode.ERROR_ALREADYEXIST);
                return(null);
            }

            if (!string.IsNullOrWhiteSpace(newName) &&
                !FilesystemHelpers.ContainsRestrictedCharacters(newName) &&
                !FilesystemHelpers.ContainsRestrictedFileName(newName))
            {
                try
                {
                    FilesystemItemType itemType       = source.IsOfType(StorageItemTypes.File) ? FilesystemItemType.File : FilesystemItemType.Directory;
                    string             originalSource = source.Path;
                    await source.RenameAsync(newName, collision);

                    errorCode?.Report(FilesystemErrorCode.ERROR_SUCCESS);
                    return(new StorageHistory(FileOperationType.Rename, StorageItemHelpers.FromPathAndType(originalSource, itemType), source.FromStorageItem()));
                }
                catch (Exception e)
                {
                    errorCode?.Report(FilesystemTasks.GetErrorCode(e));
                    return(null);
                }
            }

            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// This method only exists to provide a workaround for renaming a file to its
        /// filename as it should be defined in the GZip header. The SharpCompress library
        /// only sets the filename after the first call of <see cref="Stream.Read"/> and
        /// not when the stream instance is constructed.
        /// </summary>
        /// <param name="file">The file to be renamed.</param>
        /// <param name="stream">The possible <see cref="GZipStream"/> which holds the filename.</param>
        /// <returns>True if stream is <see cref="GZipStream"/> an file was successfully renamed.</returns>
        private static async Task <bool> GZipOutputFileNameWorkaround(IStorageItem file, Stream stream)
        {
            if (stream is GZipStream gzipStream && !string.IsNullOrEmpty(gzipStream.FileName))
            {
                await file.RenameAsync(gzipStream.FileName);

                return(true);
            }
            return(false);
        }
Exemplo n.º 4
0
        public async Task RenameAsync(string fromPath, string toPath)
        {
            //get full path from parameter "fromPath"
            fromPath = GetLocalVfsPath(fromPath);
            toPath   = GetLocalVfsPath(toPath);

            IStorageItem item = null;

            try
            {
                item = await StorageFile.GetFileFromPathAsync(fromPath);

                goto rename;
            }
            catch { }
            try
            {
                item = await StorageFolder.GetFolderFromPathAsync(fromPath);
            }
            catch { }
            if (item == null)
            {
                throw new FileNoAccessException("Can't find the item to rename");
            }

rename:
            if (Path.GetDirectoryName(fromPath) != Path.GetDirectoryName(toPath))
            {
                string        toFullPathParent  = Path.GetDirectoryName(toPath);
                StorageFolder destinationFolder = await StorageFolder.GetFolderFromPathAsync(toFullPathParent);

                if (item is IStorageFile file)
                {
                    await file.MoveAsync(destinationFolder, Path.GetFileName(toPath));
                }
                else if (item is IStorageFolder folder)
                {
                    if (!(await MoveFolder(folder, destinationFolder)))
                    {
                        throw new FileBusyException("Some items can't be moved");
                    }
                }
                else
                {
                    throw new FileBusyException("Items of unknown type can't be moved");
                }
            }
            else
            {
                await item.RenameAsync(Path.GetFileName(toPath));
            }
        }
        public static async Task <SafeWrapperResult> RenameItem(IStorageItem item, string newName, NameCollisionOption collision = NameCollisionOption.GenerateUniqueName)
        {
            SafeWrapperResult result = await SafeWrapperRoutines.SafeWrapAsync(async() => await item.RenameAsync(newName, collision).AsTask());

            return(result);
        }