Ejemplo n.º 1
0
        public ResourceHandle PerformOperation(Uri uri, ResourceOperation operation, object arg)
        {
            var sink = new FileOperationProgressSink();
            var op   = InitOperation(uri, operation, arg, sink);

            return(FinishOperation(op, sink));
        }
Ejemplo n.º 2
0
        private ResourceHandle FinishOperation(IFileOperation operation, FileOperationProgressSink sink)
        {
            operation.PerformOperations();
            //await sink.WhenCompleted();
            var item = sink.CreatedItems.LastOrDefault();

            if (item != null)
            {
                return(new ShellFileHandle(item, this));
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        ///     進捗通知クラスとオーナーウィンドウを指定して、
        ///     <see cref="FileOperation" />クラスの新しいインスタンスを初期化します。
        /// </summary>
        /// <param name="progress">進捗通知クラス。</param>
        /// <param name="ownerWindow">オーナーウィンドウ。</param>
        public FileOperation(FileOperationProgress progress, Window ownerWindow)
        {
            this.fileOperation = (IFileOperation)Activator.CreateInstance(FileOperationType);

            if (progress != null)
            {
                this.progressSink = new FileOperationProgressSink(progress);
                this.cookie       = this.fileOperation.Advise(this.progressSink);
            }

            if (ownerWindow != null)
            {
                var windowHelper = new WindowInteropHelper(ownerWindow);
                this.fileOperation.SetOwnerWindow(windowHelper.Handle);
            }
        }
Ejemplo n.º 4
0
        public Task <ResourceHandle> PerformOperationAsync(Uri uri, ResourceOperation operation, object arg, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var sink = new FileOperationProgressSink(cancellationToken);
            var op   = InitOperation(uri, operation, arg, sink);

            cancellationToken.ThrowIfCancellationRequested();
            return(Task.Run(
                       () => {
                try{
                    return FinishOperation(op, sink);
                }catch (OperationCanceledException)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    throw;
                }
            }, cancellationToken
                       ));
        }
        /// <summary>
        ///     Release all resources used by <see cref="FileOperation" /> class
        ///     and optionally releases managed resources.
        /// </summary>
        /// <param name="disposing">
        ///     <c>true</c> to release both managed and unmanaged resources.
        ///     <c>false</c> to release only unmanaged resources.
        /// </param>
        public void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                }

                // Release unmanaged resources.
                if (this.progressSink != null)
                {
                    this.fileOperation.Unadvise(this.cookie);
                    this.progressSink = null;
                }

                Marshal.FinalReleaseComObject(this.fileOperation);
                this.fileOperation = null;

                this.disposed = true;
            }
        }
Ejemplo n.º 6
0
        private IFileOperation CreateOperation(ResourceOperation operation, IShellItem source, IShellItem target, string name, FileAttributes attributes, IPropertyChangeArray properties, FileOperationProgressSink sink)
        {
            var op = Shell32.CreateFileOperation();

            op.Advise(sink);
            op.SetOwnerWindow(OwnerHwnd);
            op.SetOperationFlags(0x0400 | 0x0004 | 0x0200 | 0x00100000);
            switch (operation)
            {
            case ResourceOperation.Create:
                op.NewItem(source, attributes, name, null, null);
                break;

            case ResourceOperation.Delete:
                op.DeleteItem(source, null);
                break;

            case ResourceOperation.Move:
                if (target == null)
                {
                    op.RenameItem(source, name, null);
                }
                else
                {
                    op.MoveItem(source, target, name, null);
                }
                break;

            case ResourceOperation.Copy:
                op.CopyItem(source, target, name, null);
                break;
            }
            return(op);
        }
Ejemplo n.º 7
0
        private IFileOperation InitOperation(Uri uri, ResourceOperation operation, object arg, FileOperationProgressSink sink)
        {
            IShellItem           source = null, target = null;
            string               name       = null;
            FileAttributes       attributes = 0;
            IPropertyChangeArray properties = null;

            switch (operation)
            {
            case ResourceOperation.Create:
                attributes = (FileAttributes)arg;
                name       = HttpUtility.UrlDecode(uri.Segments[uri.Segments.Length - 1]);
                uri        = new Uri(uri, ".");
                source     = GetItem(uri);
                break;

            case ResourceOperation.Delete:
                source = GetItem(uri);
                break;

            case ResourceOperation.Move:
                source = GetItem(uri);
                name   = arg as string;
                if (name == null)
                {
                    uri    = (Uri)arg;
                    name   = HttpUtility.UrlDecode(uri.Segments[uri.Segments.Length - 1]);
                    uri    = new Uri(uri, ".");
                    target = GetItem(uri);
                }
                break;

            case ResourceOperation.Copy:
                source = GetItem(uri);
                uri    = (Uri)arg;
                name   = HttpUtility.UrlDecode(uri.Segments[uri.Segments.Length - 1]);
                uri    = new Uri(uri, ".");
                target = GetItem(uri);
                break;

            default:
                throw new NotImplementedException();
            }

            return(CreateOperation(operation, source, target, name, attributes, properties, sink));
        }