如何计算文件复制速度

如何计算文件复制速度

本文介绍了如何计算文件复制速度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btnSend_Click(object sender, EventArgs e)
        {

try
          {


              string strsource = string.Empty;
              string strDestination = string.Empty;
              string strFileName = string.Empty;

              string str = textBoxsource.Text;
              int intFileIndex = str.IndexOf(".");
              string strcut = str.Substring(str.Length - (str.Length - intFileIndex - 1), str.Length - intFileIndex - 1);
              string[] strSplit = textBoxsource.Text.Split('.');
              string strValue = strSplit[0].ToString();

              string strfilename = strValue.Substring(strValue.LastIndexOf(@"\") + 1);

              strDestination = textBoxdest.Text + strfilename + "." + strcut.ToString();
              File.Copy(str,strDestination); // Try to move/copy

              MessageBox.Show ("file moved successfully", "Demo2",MessageBoxButtons.OK); // Success


          }
          catch (IOException ex)
          {
              Console.WriteLine(ex); // Write error
          }
}

推荐答案


/// <summary>
/// Do the actual file move.
/// </summary>
/// <param name="src"></param>
/// <param name="dst"></param>
/// <param name="worker"></param>
/// <param name="prMain"></param>
private void MoveFile(string src, string dst, BackgroundWorker worker = null, ProgressReport prMain = null)
    {
    if (src != dst)
        {
        // Copy the file itself.
        int iSrc = src.IndexOf(':');
        int iDst = dst.IndexOf(':');
        FileInfo fiSrc = new FileInfo(src);
        if (fiSrc.Length < blockSize || (iSrc > 0 && iDst > 0 && iSrc == iDst && src.Substring(0, iSrc) == dst.Substring(0, iDst)))
            {
            // On same drive or trivial size - move it via the file system
            File.Move(src, dst);
            }
        else
            {
            // Needs to be moved "properly".
            using (Stream sr = new FileStream(src, FileMode.Open))
                {
                using (Stream sw = new FileStream(dst, FileMode.Create))
                    {
                    long total = sr.Length;
                    long bytes = 0;
                    long cnt = total;
                    int progress = 0;
                    while (cnt > 0)
                        {
                        int n = sr.Read(transfer, 0, blockSize);
                        sw.Write(transfer, 0, n);
                        bytes += n;
                        cnt -= n;
                        int percent = (int)((bytes * 100) / total);
                        if (progress != percent)
                            {
                            // Report progress
                            progress = percent;
                            if (worker != null && prMain != null)
                                {
                                ProgressReport pr = new ProgressReport
                                {
                                    FilesCount = prMain.FilesCount,
                                    FileNumber = prMain.FileNumber,
                                    Filename = prMain.Filename,
                                    Action = prMain.Action,
                                    FilePercentage = percent
                                };
                                worker.ReportProgress(-prMain.FileNumber, pr);
                                }
                            }
                        }
                    }
                }
            // Update the fileinfo.
            FileInfo fiDst = new FileInfo(dst);
            fiDst.Attributes = fiSrc.Attributes;
            fiDst.CreationTime = fiSrc.CreationTime;
            fiDst.CreationTimeUtc = fiSrc.CreationTimeUtc;
            fiDst.IsReadOnly = fiSrc.IsReadOnly;
            fiDst.LastAccessTime = fiSrc.LastAccessTime;
            fiDst.LastAccessTimeUtc = fiSrc.LastAccessTimeUtc;
            fiDst.LastWriteTime = fiSrc.LastWriteTime;
            fiDst.LastWriteTimeUtc = fiSrc.LastWriteTimeUtc;
            // And remove the source
            File.Delete(src);
            }
        }
    }

从中得到完成估计值相对微不足道。

It would be relatively trivial to get a "completion" estimate from that.


这篇关于如何计算文件复制速度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-19 18:58