Esempio n. 1
0
        private void backgroundWorkerCopy_DoWork(object sender, DoWorkEventArgs e)
        {
            int counter = 0;

            backgroundWorkerCopy.ReportProgress(0);
            amountOfBytesCopied = 0;
            byte[] buffer = new byte[bufferLenght];
            if (destinationPath != null && destinationSystemUtility != null && !destinationSystemUtility.DirectoryExists(destinationPath))
            {
                destinationSystemUtility.CreateDirectory(destinationPath);
            }
            startDateTime = DateTime.Now;
            for (int i = 0; i < sourceFiles.Length; i++)
            {
                if (backgroundWorkerCopy.CancellationPending)
                {
                    e.Cancel = true;
                    e.Result = counter;
                    return;
                }
                string destinationFile = null;
                if (destinationPath != null)
                {
                    if (useDateTimeForFileNames == false)
                    {
                        destinationFile = System.IO.Path.Combine(destinationPath, System.IO.Path.GetFileName(sourceFiles[i]));
                    }
                    else
                    {
                        destinationFile = System.IO.Path.Combine(destinationPath, GetNewFileName()) + System.IO.Path.GetExtension(sourceFiles[i]);
                        int index = 1;
                        while (System.IO.File.Exists(destinationFile))
                        {
                            destinationFile = System.IO.Path.Combine(destinationPath, GetNewFileName() + " (" + (++index).ToString() + ")") + System.IO.Path.GetExtension(sourceFiles[i]);
                        }
                    }
                }
                else if (destinationFiles != null)
                {
                    destinationFile = destinationFiles[i];
                }

                if (saveAction != null)
                {
                    try
                    {
                        saveAction(sourceFiles[i]);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("خطا در ذخیره فایل" + "\r\n" + sourceFiles[i] + "\r\n" + "\r\n" + ex.Message);
                    }
                    System.IO.FileInfo fileInfo = new System.IO.FileInfo(sourceFiles[i]);
                    amountOfBytesCopied += fileInfo.Length;
                    backgroundWorkerCopy.ReportProgress((int)(100 * amountOfBytesCopied / totalLenght), sourceFiles[i]);
                }
                else
                {
                    System.IO.FileStream sourceStream      = null;
                    System.IO.FileStream destinationStream = null;
                    try
                    {
                        sourceStream      = System.IO.File.OpenRead(sourceFiles[i]);
                        destinationStream = destinationSystemUtility.CreateFile(destinationFile);
                        int readCount;
                        do
                        {
                            if (backgroundWorkerCopy.CancellationPending)
                            {
                                e.Cancel = true;
                                if (sourceStream != null)
                                {
                                    sourceStream.Close();
                                    sourceStream.Dispose();
                                }
                                if (destinationStream != null)
                                {
                                    destinationStream.Close();
                                    destinationStream.Dispose();
                                }
                                try
                                {
                                    destinationSystemUtility.DeleteFile(destinationFile);
                                }
                                catch { }
                                e.Result = counter;
                                return;
                            }
                            readCount = sourceStream.Read(buffer, 0, buffer.Length);
                            destinationStream.Write(buffer, 0, readCount);

                            amountOfBytesCopied += readCount;
                            backgroundWorkerCopy.ReportProgress((int)(100 * amountOfBytesCopied / totalLenght), sourceFiles[i]);
                        }while (readCount > 0);
                    }
                    finally
                    {
                        if (sourceStream != null)
                        {
                            sourceStream.Close();
                            sourceStream.Dispose();
                        }
                        if (destinationStream != null)
                        {
                            destinationStream.Close();
                            destinationStream.Dispose();
                        }
                    }
                }
                counter++;
            }
            backgroundWorkerCopy.ReportProgress(100);
            e.Result = counter;
        }
Esempio n. 2
0
        private void backgroundWorkerCopy_DoWork(object sender, DoWorkEventArgs e)
        {
            backgroundWorkerCopy.ReportProgress(0);
            amountOfBytesCopied = 0;
            byte[] buffer = new byte[bufferLenght];
            if (destinationPath != null && !destinationSystemUtility.DirectoryExists(destinationPath))
            {
                destinationSystemUtility.CreateDirectory(destinationPath);
            }
            startDateTime = DateTime.Now;
            for (int i = 0; i < sourceFiles.Length; i++)
            {
                if (backgroundWorkerCopy.CancellationPending)
                {
                    e.Cancel = true;
                    return;
                }
                string destinationFile;
                if (destinationPath != null)
                {
                    destinationFile = System.IO.Path.Combine(destinationPath, System.IO.Path.GetFileName(sourceFiles[i]));
                }
                else
                {
                    destinationFile = destinationFiles[i];
                }
                if (this.overWrite || (!this.overWrite && !destinationSystemUtility.FileExists(destinationFile)))
                {
                    System.IO.FileStream sourceStream      = null;
                    System.IO.FileStream destinationStream = null;
                    try
                    {
                        sourceStream      = sourceSystemUtility.OpenFile(sourceFiles[i]);
                        destinationStream = destinationSystemUtility.CreateFile(destinationFile);
                        int readCount;
                        do
                        {
                            if (backgroundWorkerCopy.CancellationPending)
                            {
                                e.Cancel = true;
                                if (sourceStream != null)
                                {
                                    sourceStream.Close();
                                    sourceStream.Dispose();
                                }
                                if (destinationStream != null)
                                {
                                    destinationStream.Close();
                                    destinationStream.Dispose();
                                }
                                try
                                {
                                    destinationSystemUtility.DeleteFile(destinationFile);
                                }
                                catch { }
                                return;
                            }
                            readCount = sourceStream.Read(buffer, 0, buffer.Length);
                            destinationStream.Write(buffer, 0, readCount);

                            amountOfBytesCopied += readCount;
                            backgroundWorkerCopy.ReportProgress((int)(100 * amountOfBytesCopied / totalLenght), sourceFiles[i]);
                        }while (readCount > 0);
                    }
                    finally
                    {
                        if (sourceStream != null)
                        {
                            sourceStream.Close();
                            sourceStream.Dispose();
                        }
                        if (destinationStream != null)
                        {
                            destinationStream.Close();
                            destinationStream.Dispose();
                        }
                    }
                }
            }
            backgroundWorkerCopy.ReportProgress(100);
        }