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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:11:06+00:00 2026-05-27T01:11:06+00:00

I know the answer must be out there somewhere, I applied suggestions both from

  • 0

I know the answer must be out there somewhere, I applied suggestions both from many other questions and from MSDN itself but I’m probably overlooking something here.

This is my method, I use it to dump output to file. lock object declaration attached for clarity.

private static Object fileLock = new Object();
private static void WriteToFile(string msg, bool WriteLine)
{
    lock (fileLock)
    {
        msg = DateTime.Now.ToShortTimeString() + " - " + msg;

        FileInfo F = new FileInfo("dump.txt");
        using (StreamWriter writer = F.Exists ? F.AppendText() : F.CreateText()) //<--THIS LINE THROWS
        {
            if (WriteLine)
                writer.WriteLine(msg);
            else
                writer.Write(msg);
        }
    }
}

Question is: Why does the using line above throws an IOException complaining another process is using the file the 2nd time I call the method ?

I’m calling it like this around my code:

Console.WriteLine(something)
#if(DEBUG)
    Extensions.WriteToFile(something,true);
#endif

Again, I’m sure this is a trivial issue and someone else asked something like this getting the right answer, but I’m unable to dig it up.

UPDATE

Refactoring out the FileInfo object and switching to File.XXX methods made the code work fine. I still wonder what the issue was, anyway the issue looks like solved.

  • 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-27T01:11:07+00:00Added an answer on May 27, 2026 at 1:11 am

    @Guffa: declaration has to be private static object fileLock = new object();

    @alex: Your code works just fine on my machine although it’s a bit too complicated for the task imo.

    static void Write(string text, string file)
        {
            using (StreamWriter sw = File.AppendText(file))// Creates or opens and appends
            {
                sw.WriteLine(text);
            }
        }
    

    Maybe some antivirus or indexer locks your dump file.

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

Sidebar

Related Questions

I know this answer must be out there, but I can't figure out how
Just a quick question that no doubt someone out there will know the answer
I'm almost certain I know the answer to this question, but I'm hoping there's
I've scoured the Internet but my search-foo must not work if it's out there
I know it must be something simple, but I just can't figure it out..
I know that somewhere in the history of the internet this must have been
I know the answer depends on the particular JVM, but I would like to
I know the answer for the beta was no ( Can I use .NET
I think I know the answer, but I would like to bounce around some
I believe I already know the answer, but I am not 100% sure, so

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.