Click here to Skip to main content
15,867,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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
          }
}
Posted

Use a Stopwatch[^] to measure the time elapsed and then divide the file size by the time elapsed itself.
 
Share this answer
 
You can't, not easily - look at the trouble MS have estimating file copy duration:
http://xkcd.com/612/[^] sums it up well.

There are a huge number of variables: the read speed of the source, the write speed of the destination, any buffering the two of the do internally, any buffering Windows does, any networking between them, cache usage, memory available, and other tasks activities can all affect it one way of the other.
What you can do is do the copy yourself, and use a stopwatch to time how far it's got - that lets you make an estimate of when it will finish, but it will be very much a moving target!

This is the code I use to report progress in a background worker while moving a file:
C#
/// <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.
 
Share this answer
 
Comments
Member 10849000 28-May-14 5:50am    
this particular code is giving error , may i know what is the ProgressReport and how to set the blockSize to griff
regards,
rakesh

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900