コード例 #1
0
        private async Task <string> FileTransfer(string sourcePath, string targetPath, string checksum, string tmpFilePath, bool resumeProcessing,
                                                 CancellationToken cancellationToken)
        {
            var credentials = _userAccessCredentials.ReadCredentials();

            _totalBytes      = 0;
            _sizeOfFile      = 0;
            _processingSpeed = 0;
            if (!targetPath.Contains(".tmp"))
            {
                targetPath = $"{targetPath}.tmp";
            }
            using (var connection = _networkConnection.Connect(_networkPathInfo.GetNetworkPath(), credentials.Login, credentials.Password))
            {
                try
                {
                    byte[] buffer = new byte[16384];
                    int    readBytes;

                    if (File.Exists(targetPath))
                    {
                        File.SetAttributes(targetPath, File.GetAttributes(targetPath) & ~FileAttributes.Hidden);
                    }
                    using (FileStream sourceStream = File.OpenRead(sourcePath))
                        using (FileStream targetStream = File.Create(targetPath))
                        {
                            if (targetStream.Length == sourceStream.Length)
                            {
                                ProcessingFinishedDelegate?.Invoke(true, null);
                                return(string.Empty);
                            }
                            File.SetAttributes(targetPath, File.GetAttributes(targetPath) | FileAttributes.Hidden);
                            _sizeOfFile = sourceStream.Length;

                            using (var timer = new System.Threading.Timer(FormStatusUpdate, null, 1000, 1000))
                            {
                                while ((readBytes = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
                                {
                                    if (cancellationToken.IsCancellationRequested)
                                    {
                                        cancellationToken.ThrowIfCancellationRequested();
                                    }
                                    await targetStream.WriteAsync(buffer, 0, readBytes);

                                    _totalBytes += readBytes;
                                }
                            }
                            File.SetAttributes(targetPath, File.GetAttributes(targetPath) & ~FileAttributes.Hidden);
                            ProcessingFinishedDelegate?.Invoke(true, null);
                        }
                    string newPath = $"{ Path.GetDirectoryName(targetPath)}\\{Path.GetFileNameWithoutExtension(targetPath)}";
                    File.Move(targetPath, newPath);
                    return(newPath);
                }
                catch (Exception ex)
                {
                    if (resumeProcessing == true)
                    {
                        SaveFileInfo(checksum, _totalBytes, tmpFilePath, Path.GetFileName(targetPath));
                    }
                    else
                    {
                        File.Delete(targetPath);
                        DeleteRowFromMetaDataFile(checksum, tmpFilePath);
                    }
                    ProcessingFinishedDelegate?.Invoke(false, ex);
                }
                return(string.Empty);
            }
        }