Beispiel #1
0
        /// <summary>
        /// Technical File Copy, using stream and buffer to improve perf on large files and allow progressCallback
        /// Can be optimised for smaller file
        /// TODO: Set the buffer size as parameter
        /// </summary>
        /// <param name="file">Files to copy</param>
        /// <param name="progressCallback">give the information on file copy progress</param>
        /// <returns>Task to manage asynch</returns>
        public static async Task CopyData(scFile file, Action <string, Constants.Status, long> progressCallback)
        {
            var from = file.FromName;
            var to   = file.ToName;

            using (var outStream = new FileStream(to, FileMode.Create, FileAccess.Write, FileShare.Read))
            {
                using (var inStream = new FileStream(from, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    //await inStream.CopyToAsync(outStream);

                    try
                    {
                        int    bufferLength = 1280;
                        byte[] buffer       = new byte[bufferLength];
                        int    bytesRead    = 0;
                        int    iIteration   = 1;
                        do
                        {
                            bytesRead = inStream.Read(buffer, 0, bufferLength);
                            outStream.Write(buffer, 0, bytesRead);
                            progressCallback(file.FromName, Constants.Status.InProgress, iIteration * bufferLength);
                        }while (bytesRead != 0);
                    }
                    catch (Exception)
                    {
                        progressCallback(file.FromName, Constants.Status.CopyError, 0);
                        throw;
                    }
                }
            }

            progressCallback(file.FromName, Constants.Status.Finished, file.FileSize);
        }
        public void add(string sEntry, string sOriginDir, string sDestDir, Constants.Action aAction)
        {
            try
            {
                // get the file attributes for file or directory
                FileAttributes attr = File.GetAttributes(sEntry);

                //detect whether its a directory or file
                if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    scDir         dir = new scDir();
                    DirectoryInfo di  = new DirectoryInfo(sEntry);
                    dir.FromName = di.FullName;
                    dir.ToName   = di.FullName.Replace(sOriginDir, sDestDir);
                    dir.Action   = aAction;
                    this._lfDirs.Add(dir);
                }
                else
                {
                    scFile   file = new scFile();
                    FileInfo fi   = new FileInfo(sEntry);
                    file.FileSize = fi.Length;
                    file.FromName = fi.FullName;
                    file.ToName   = fi.FullName.Replace(sOriginDir, sDestDir);
                    file.Action   = aAction;
                    this._lfFiles.Add(file);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }