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

  • Home
  • SEARCH
  • 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 9161491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:58:43+00:00 2026-06-17T13:58:43+00:00

I have a StreamWriter object to write to a log file, it is initialised

  • 0

I have a StreamWriter object to write to a log file, it is initialised as follows:

StreamWriter streamWriter = File.AppendText(GetLogFilePath());
streamWriter.AutoFlush = true;

Later in my code, I need to close the log file, and then read in part of the logged out contents. I have the following code:

streamWriter.Close();
streamWriter = null;
StreamReader logFileStream = File.OpenText(GetLogFilePath());

This is throwing an exception:
The process cannot access the file because it is being used by another process.

Presumably, the problem is that the log file is not correctly closed when I close the StreamWriter.

Is there a better way to close a StreamWriter to ensure that the file it has open, is correctly closed?

UPDATE: I have resolved my issue. It was caused by an unrelated problem (where a separate application was accessing the file that I was trying to access).
I am accepting Dmitry Martovoi’s answer, as I think it is the most useful for other people who have a similar issue in the future; but an important note is that the solution does require the log file to be opened every time the log is written to, which can cause unnecessary overhead.

  • 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-17T13:58:44+00:00Added an answer on June 17, 2026 at 1:58 pm

    I always wrap such code in a using statement

    using (StreamWriter writer = File.AppendText(path)) 
    {
       writer.WriteLine("This");
       writer.WriteLine("is Extra");
       writer.WriteLine("Text");
    }
    

    but for StreamWriter Close method doing exactly the same as Dispose.

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

Sidebar

Related Questions

I'm attempting to mock some file operations. In the real object I have: StreamWriter
I have this code FileStream fs = new FileStream(Scores.txt, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw =
I have an example in C# code, but it is using streamWriter . It
I have this piece of code in c#: private static void _constructRow(SqlDataReader reader, system.IO.StreamWriter
Okay, so i have this code i wrote: class Connection { public static StreamWriter
I have this button click code wich is working now: private void button6_Click(object sender,
I have such code so far: private void button3_Click(object sender, EventArgs e) { label1.Visible
I have a multi threaded app that writes log to file. occasionaly, saving fails
I have a timer function that serialize an object into a xml file once
I am using a memorystream object to write values from streamwriter object into memory.

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.