Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8249417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:32:15+00:00 2026-06-07T23:32:15+00:00

Here is my code: public static TextWriter twLog = null; private int fileNo =

  • 0

Here is my code:

public static TextWriter twLog = null;
private int fileNo = 1;
private string line = null;

TextReader tr = new StreamReader("file_no.txt");
TextWriter tw = new StreamWriter("file_no.txt");
line = tr.ReadLine();
if(line != null){
    fileNo = int.Parse(line);
    twLog = new StreamWriter("log_" + line + ".txt");
}else{
    twLog = new StreamWriter("log_" + fileNo.toString() + ".txt");  
}
System.IO.File.WriteAllText("file_no.txt",string.Empty);
tw.WriteLine((fileNo++).ToString());
tr.Close();
tw.Close();
twLog.Close();

It throws this error:

IOException: Sharing violation on path C:\Users\Water Simulation\file_no.txt

What i’m trying to do is just open a file with log_x.txt name and take the “x” from file_no.txt file.If file_no.txt file is empty make log file’s name log_1.txt and write “fileNo + 1” to file_no.txt.After a new program starts the new log file name must be log_2.txt.But i’m getting this error and i couldn’t understand what am i doing wrong.Thanks for help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T23:32:18+00:00Added an answer on June 7, 2026 at 11:32 pm

    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 😀

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the code: public static async Task<string> DownloadPageWithCookiesAsync(string url) { HttpClientHandler handler = new
So here is my code: public static void getArmor(String treasure) throws FileNotFoundException{ Random rand=new
Here is the code: public static void main(String[] args) { SocketWorker worker = null;
Here is my code: public static string ReadListViewItem(IntPtr lstview, int item) { const int
Here's my JFrame code: public static void main(String[] args) { JFrame jf = new
Here is my code: public static void decodeThis(BufferedImage im, String outputFile) throws FileNotFoundException{ int
I have this code here: public static String AddRemoveDays(String date, int days) throws ParseException
here is the code public static final String DATE_TIME_FORMAT = yyyy-MM-dd kk:mm:ss; public static
Here is my code : public static void main(String[] args) { // System.setProperty( //
Here is my code: public class Test { static { main(null); } public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.