Sharing violation IOException while reading and writing to file C#

Well, you’re trying to open the file file_no.txt for reading and for writing using separate streams. This may not work as the file will be locked by the reading stream, so the writing stream can’t be created and you get the exception.

One solution would be to read the file first, close the stream and then write the file after increasing the fileNo. That way the file is only opened once at a time.

Another way would be to create a file stream for both read and write access like that:

FileStream fileStream = new FileStream(@"file_no.txt", 
                                       FileMode.OpenOrCreate, 
                                       FileAccess.ReadWrite, 
                                       FileShare.None);

The accepted answer to this question seems to have a good solution also, even though I assume you do not want to allow shared reads.

Possible alternate solution
I understand you want to create unique log files when your program starts. Another way to do so would be this:

int logFileNo = 1;
string fileName = String.Format("log_{0}.txt", logFileNo);

while (File.Exists(fileName))
{
    logFileNo++;
    fileName = String.Format("log_{0}.txt", logFileNo);
}

This increases the number until it finds a file number where the log file doesn’t exist. Drawback: If you have log_1.txt and log_5.txt, the next file won’t be log_6.txt but log_2.txt.

To overcome this, you could enumerate all the files in your directory with mask log_*.txt and find the greatest number by performing some string manipulation.

The possibilities are endless 😀