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 6091529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:20:19+00:00 2026-05-23T12:20:19+00:00

My program is logging data to a file, at the same time a user

  • 0

My program is logging data to a file, at the same time a user interface displays the incoming data live. I want the logged data to be on disk within a second or two if computer/program/os/whatever shuts down. Data is coming in at least 100 times/sec.

I want the user to be able to give the log-file a new name, while logging is active. The problem is that i can’t change the name of the file while it is open, even if it is by the same process.

Test case:

string fileName1 = "test.txt";
string fileName2 = "test2.txt";

using (StreamWriter sw = new StreamWriter(new FileStream(fileName1, FileMode.Create)))
{
     sw.WriteLine("before");
     File.Move(fileName1, fileName2);  //<<-- IOException - The process cannot access the file because it is being used by another process.
     w.WriteLine("after");
 }

So, How do i rename a file from a process while the same process is having a stream to the file open?

  • 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-05-23T12:20:19+00:00Added an answer on May 23, 2026 at 12:20 pm

    You should close the first stream, rename the file, then reopen the stream:

    using (StreamWriter sw = new StreamWriter(new FileStream(fileName1, FileMode.Create)))
    {
      sw.WriteLine("before");
      sw.Close();
    }
    
    File.Move(fileName1, fileName2);
    
    using (StreamWriter sw = new StreamWriter(new FileStream(fileName2, FileMode.Append)))
    {
      sw.WriteLine("after");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a command line program, which outputs logging to the screen. I want
I have created a small program which logs text data to a file on
I have created a file from my java programming, and am logging some data
I'm trying to enhance my program with some logging functionality provided by System.Diagnostics .
My iPhone app started logging this error: lo->hi recycling invariant violated! followed by: Program
What program can I use to decompile a class file? Will I actually get
I'm writing a program for converting media file to mp3 file with GStreamer. It
I need some clever logging library for a daemon-like C (not C++ ) program
I want to use same database dir on my ubuntu installation as of windows
I am writing a program to parse a WAV file header and print the

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.