Click here to Skip to main content
15,898,664 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
i want to display the current progress..
So, In the first line it should display the current file Name
second line to display the current file No.
third should display completed %

So i made this little program

C#
string[] files = Directory.GetFiles(sourceDirectory, "*", SearchOption.AllDirectories);
int totalfiles = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories).Length;
Console.WriteLine("{0} File(s) Found.", totalfiles); //For Example Found 1000 Files
int num = 1;
double percent = 0.0;
foreach (string file in files)
{
Console.Write("\rCurrent File Name :- {0}",Path.GetFileName(file));
percent = Convert.ToInt32(((double)num / (double)totalfiles) * (double)100);
Console.Write("\r{0}/{1} Files Completed", num++, totalfiles);
Console.Write("\r{0}% Completed", (num / totalfiles) * 100);
Thread.Sleep(10);
//DoingSomeWork();
}


But This is not displaying it correctly as i wanted

SeeThisOutputImage

it if overlapping everyline..
i know that i am not writing "\n" or "WriteLine" as this will generate the new line every time
so what should i do??
Please Help

Sorry for my bad English and also sorry if the question title is not correct..
Posted
Updated 11-Dec-15 17:29pm
v6
Comments
BillWoodruff 11-Dec-15 23:19pm    
Please show an example of the current output which is not displayed correctly.
Palash Sachan 11-Dec-15 23:28pm    
i have updated the ques sir..please see https://goo.gl/CXty8c
PIEBALDconsult 11-Dec-15 23:22pm    
0) Don't call Directory.GetFiles a second time ; use files.Length -- and why use two different patterns?
1) Use \n for a linefeed; not \r .
2) Why use Write if you want a new line anyway? Why not use WriteLine?
3) Don't use Convert. Ever.
4) Don't caclculate the percentage twice.
5) No need to cast to double; try num * 100.0 / totalfiles .
6) Why sleep? Especially for so short time?
7) Don't use foreach when for will do what you need faster.
BillWoodruff 11-Dec-15 23:24pm    
I'd say that's a prescription that would merit a #5 if posted as a solution !
Palash Sachan 11-Dec-15 23:44pm    
dear sir..edited all things you said
except for using "WriteLine" or "\n" instead of "Write" or "\r"
i m using Write so that it do not create the new line everytime
and i m using \r so that the line is updated evertime
now pls give any solution to my problem sir
thanks

1 solution

My guess would be, that you are looking for
Console.SetCursorPosition
for further reading maybe this[^] would also help.
 
Share this answer
 
Comments
Palash Sachan 12-Dec-15 3:20am    
working perfectly by giving left and top position and also b adding this line
Console.Write("\r" + new string(' ', Console.WindowWidth) + "\r");
thanks sir :)

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