Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
how do i make a update in percentage of copy and paste progress in c# console

i write this but this is not working perfectly..it only shows the 100% when the file is full copied

C#
string fileName = @"abc.mp4";
string sourcePath = @"C:\test\source";
string targetPath = @"C:\test\copy";

string sourceFile = Path.Combine(sourcePath, fileName);
string destFile = Path.Combine(targetPath, fileName);

if (!Directory.Exists(targetPath))
{
     Directory.CreateDirectory(targetPath);
}

Console.WriteLine("Coping a File Started at {0}", DateTime.Now);

FileInfo file = new FileInfo(sourcePath.ToString() + "\\" + fileName.ToString());

Console.WriteLine("The File Size is {0} MB", ((file.Length)/1024)/1024);

FileInfo sourcesize = new FileInfo(sourcePath.ToString() + "\\" + fileName.ToString());
            
FileInfo destsize = new FileInfo(targetPath.ToString() + "\\" + fileName.ToString());

            double si = 0.0;

            while (si != 100)
                {
                    File.Copy(sourceFile, destFile, true);

                    si = (destsize.Length / sourcesize.Length) * 100;

                    Console.Write("{0} % Completed", si);
                }



now i want is that it should update the percent while it is copying the file

please help
Posted
Updated 15-Nov-15 19:14pm
v3
Comments
Krunal Rohit 16-Nov-15 1:37am    
Of course. Loop is getting executed that fast. Try with debugger and check the si variable on each iteration of the loop.

-KR
Palash Sachan 16-Nov-15 1:42am    
i checked..what it is doing is when it copies the whole file then it moves further and so the si is 100% shown..the wrong thing is i think file.copy..is it??
Sergey Alexandrovich Kryukov 16-Nov-15 2:13am    
Not really a question. Yes, you will always get 0% and then 100%, so what? Why would you need to progress of such operation?
—SA

1 solution

Yes, you will always get 0% and then 100%, because this call is just one operations which is started at 0% and the method returns, it's already 100%. You can do it in parts only of you implement your own copy. Open source and target files and copy data in smaller chunks. Then you would be able to update the percentage inside the loop. Note that it will most likely reduce copy performance, and even considerably. I would not do it, or maybe only for some study exercise.

—SA
 
Share this answer
 
Comments
Palash Sachan 16-Nov-15 3:24am    
okay if this will slow down the copy speed then how should i do it? i have got this projject to copy and paste the file and display the current progress..please help if u can..thanks
Sergey Alexandrovich Kryukov 16-Nov-15 10:13am    
What do you mean "how"? Which part is unclear to you? Say, use System.IO.BinaryReader with System.IO.BinaryWriter:
https://msdn.microsoft.com/en-us/library/system.io.binaryreader%28v=vs.110%29.aspx,
https://msdn.microsoft.com/en-us/library/system.io.binarywriter(v=vs.110).aspx.

—SA

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