The error message says it all: the file you are trying to read is in use.
When you open a file for writing, the stream acquires an "exclusive lock" on the file to prevent the contents changing while it is being read and potentially invalidating the data.
When another stream tries to open the same file, it gets rejected until the original stream has been closed.
The most likely reason you are getting this is that somewhere else in your app you are opening the file and not closing it properly, even if the stream itself is no longer accessible.
The best way to avoid this is to use a
using
block around all stream creation, so that the system will close and dispose the stream when you are finished with it and it goes out of scope:
private void writeText(string strPath, TimeSpan tsDuration)
{
using (StreamWriter str = new StreamWriter(strPath, true))
{
str.WriteLine(tsDuration/suma);
sort(strPath);
}
}
But in your case, because you are trying to sort the file you have open that won't help you fix this.
I'd strongly suggest that you start thinking about using a database instead of text files - they provide much better facilities for this kind of work than flat text files do.
Oh, and by the way - your sort code does nothing useful at all: sorting lines you read into memory doesn't affect the file content in any way, and the sorted data is discarded as soon as your
sort
method exits ...