Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 8 如何在windows 8或8.1上取消复制事务?_Windows 8 - Fatal编程技术网

Windows 8 如何在windows 8或8.1上取消复制事务?

Windows 8 如何在windows 8或8.1上取消复制事务?,windows-8,Windows 8,我正在尝试通过StorageFile.CopySync复制文件,有时我需要在复制一个大文件时取消它,怎么做?看起来你在发布后一小时发现了一个重复的问题-这对你来说解决了吗?我为StorageFile编写了一个扩展方法,请查看下面的代码回答。 public static class FileExtentions { #region Fields private static readonly ulong MaxBufferSize = 8 * 1024 * 1024; #e

我正在尝试通过StorageFile.CopySync复制文件,有时我需要在复制一个大文件时取消它,怎么做?

看起来你在发布后一小时发现了一个重复的问题-这对你来说解决了吗?我为StorageFile编写了一个扩展方法,请查看下面的代码回答。
public static class FileExtentions
{
    #region Fields
    private static readonly ulong MaxBufferSize = 8 * 1024 * 1024;
    #endregion // Fields

    #region Methods
    public static async Task<StorageFile> CopyAsync(this StorageFile self, 
        StorageFolder desiredFolder, string desiredNewName, CreationCollisionOption option, CancellationTokenSource cancelToken)
    {
        StorageFile desiredFile = await desiredFolder.CreateFileAsync(desiredNewName, option);
        StorageStreamTransaction desiredTransaction = await desiredFile.OpenTransactedWriteAsync();
        BasicProperties props = await self.GetBasicPropertiesAsync();
        IInputStream stream = await self.OpenSequentialReadAsync();

        try
        {
            ulong copiedSize = 0L;
            while (copiedSize < props.Size)
            {
                if (cancelToken.IsCancellationRequested)
                {
                    break;
                }
                else
                {
                    ulong bufferSize = (props.Size - copiedSize) >= MaxBufferSize ? MaxBufferSize : props.Size - copiedSize;
                    IBuffer buffer = BytesToBuffer(new byte[bufferSize]);
                    await stream.ReadAsync(buffer, (uint)bufferSize, InputStreamOptions.None);
                    await desiredTransaction.Stream.GetOutputStreamAt(copiedSize).WriteAsync(buffer);
                    await desiredTransaction.Stream.GetOutputStreamAt(copiedSize).FlushAsync();
                    buffer = null;
                    copiedSize += (bufferSize);
                }
            }
        }
        catch (Exception exc)
        {
            Logger.Log.Warn(exc);
        }

        await desiredTransaction.CommitAsync();

        if (cancelToken.IsCancellationRequested)
        {
            await desiredFile.DeleteAsync();
        }

        return desiredFile;
    }

    private static IBuffer BytesToBuffer(byte[] bytes)
    {
        using (var dataWriter = new DataWriter())
        {
            dataWriter.WriteBytes(bytes);
            return dataWriter.DetachBuffer();
        }
    }
    #endregion // Methods