Click here to Skip to main content
15,671,383 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I would like to create a time entry in the text file in the form of a ranking. Ranking would be sorted by time value. I create WPF Application. What I created will only write time and further writing to the file is no longer possible.
Does anyone have an idea?

Thank you all for any advice

What I have tried:

TimeSpan duration = TimeSpan.FromMilliseconds(mt.ElapsedMilliseconds);
MessageBox.Show("Your time is" + duration.ToString("mm\\:ss\\.ff") + "\n" + "Number of example: " + numberOfExample.Text + "\n" + "Average time: " + Math.Round(duration.TotalSeconds / suma, 4) + " seconds");          
            StreamWriter str;
            if (File.Exists(path))
                str = new StreamWriter(path, false);
                str = new StreamWriter(path, true);

Creating a StreamReader is not a problem, I don't know how to proceed to save the time according to its value in the created text file, in such an order that it creates a ranking.
Updated 17-May-21 23:22pm
OriginalGriff 18-May-21 3:11am    
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
Member 15170612 18-May-21 3:27am    
sorry i will try to improve it
Member 15170612 18-May-21 3:53am    
Is it better?
Richard MacCutchan 18-May-21 3:18am    
You need to read all the entries in the file and sort them in order.
Member 15170612 18-May-21 4:42am    
so again StreamReader reads a text file and then edits it like like MessageBox?

1 solution

so again StreamReader reads a text file and then edits it like like MessageBox?

No ... a StreamReader does what it says: reads a file from the beginning to the end.

And the problem you have is that text files have no organization: you can't insert text into the middle of a text file, you have to create a new file, copy everything up to the insertion point from the old to the new, write your new data, then copy everything else from the old file into the new, close them both, delete the original, and rename the new file.

And given that you don't know where in the file your data will be ... or how long your entries are ... it's going to be difficult to do that. To make things worse, your time - the data you want to sort on - is now a string, so it will get organized by the string sort order not a time-based one.

The way I'd do it is to store it in a less human readable form: create a class that holds the duration, the example number, and the duration as native numeric values rather than strings. Then I'd create a List of that class, and read / write it to the file via JSON.
Nobody is going to look at the file anyway, and it's a lot easier to sort / search / filter the data if it's in numeric based values rather than mixed into a string somewhere in a text file!

Sounds complicated? It isn;t, not really - reading and writing a whole List<MyClass> to / from a JSON file is one line of code!
Share this answer
Member 15170612 18-May-21 8:24am    
Thank you so much for the advice. I will try it.

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