Hello, I'm trying to combine two of my methods into one to save space and improve functionality. Can anyone advise me how to combine these two codes so that they work? The first method is to write the line number at the beginning of the text document, the second method is to write the duration to the text document. I would like each line to have a number to show how much time it is.
EDIT2:
Can someone advise me how to add line numbering to my code?
What I have tried:
public void lineNumber()
{
{
StreamReader sr1 = File.OpenText(path);
string s = "";
int counter = 1;
StringBuilder sb = new StringBuilder();
while ((s = sr1.ReadLine()) != null)
{
var lineOutput = counter++ + " " + s;
sb.Append(lineOutput);
}
sr1.Close();
StreamWriter sw1 = File.AppendText(path);
sw1.Write(sb);
sw1.Close();
}
}
and
private void writeText(string strPath, TimeSpan tsDuration)
{
var list = new List<TimeSpan>();
string line;
var streamReader = new StreamReader(strPath);
while ((line = streamReader.ReadLine()) != null)
{
list.Add(TimeSpan.Parse(line));
}
list.Add(tsDuration / suma);
list = list.OrderBy(i => i).ToList();
streamReader.Close();
StreamWriter streamWriter = new StreamWriter(strPath, false);
foreach (var item in list)
{
streamWriter.WriteLine(item);
}
streamWriter.Close();
}
EDIT:
private List<TimeSpan> list;
private void writeText(string strPath, TimeSpan tsDuration)
{
readFile(strPath, tsDuration);
writeFile(strPath);
}
private void readFile(string strPath, TimeSpan tsDuration)
{
string line;
var streamReader = new StreamReader(strPath);
while ((line = streamReader.ReadLine()) != null)
{
list.Add(TimeSpan.Parse(line));
}
list.Add(tsDuration / suma);
list = list.OrderBy(i => i).ToList();
streamReader.Close();
}
private void writeFile(string strPath);
{
StreamWriter streamWriter = new StreamWriter(strPath, false);
foreach (var item in list)
{
streamWriter.WriteLine(item);
}
streamWriter.Close();
}
EDIT2:
I don't know how to do it at all, I'll be very happy for advice.
Thank you for any advice!