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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:42:12+00:00 2026-06-11T18:42:12+00:00

When I create my log.txt with File.Create(Path.Combine(PATH, NAME)); and then try to read from

  • 0

When I create my log.txt with File.Create(Path.Combine(PATH, NAME)); and then try to read from it I get an exception: {System.IO.IOException: The process cannot access the file 'c:\temp\log.txt' because it is being used by another process..

If the log.txt file exits and not created in the method I can read and write to the log wihtout any problems.

Is the log.txt created async and the problem is that the program is trying to read it before it’s created?

public static void WriteToLog(string text)
{
    try
    {
        if (!Directory.Exists(PATH))
        {
            Directory.CreateDirectory(PATH);
        }

        if( !File.Exists(Path.Combine(PATH, NAME)) )
        {
            File.Create(Path.Combine(PATH, NAME));
        }

        var logLines = File.ReadAllLines(Path.Combine(PATH, NAME)).ToList<string>();
        logLines.Insert(0, "-------------------------------------------------End New Log");
        logLines.Insert(0, text);
        logLines.Insert(0, "-------------------------------------------------Start New Log");
        File.WriteAllLines(Path.Combine(PATH, NAME), logLines);
    }
    catch (Exception ex)
    {
    }
}
  • 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-11T18:42:13+00:00Added an answer on June 11, 2026 at 6:42 pm

    File.Create creates a filestream, which is open after the creation. so the file is used by its own process.

    just change it to

            using(var f = File.Create(Path.Combine(PATH, NAME))) { } ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read some text from a .txt file, here's my code: String
I want to create log file in the format (Log + datetime.Now).txt in my
I want to create log file. this is the code that i try to
I want to create log file for my flex application. That is the file
I need to create a log file containing HTML (tables, etc). It seems that
I have to create a log file for all internet connections made by PC.
I am trying to create a simple log in system that uses ajax but
I'm trying to create a new log file every hour with the following code
I am trying to use 'git log --pretty=tformat' to create xml file log. However
The code has been updated to reference the changes below. This log system create

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.