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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:36:09+00:00 2026-06-08T17:36:09+00:00

I am getting this error: The process cannot access the file (…) because it

  • 0

I am getting this error: The process cannot access the file (…) because it is being used by another process.
I have tried to use
File.WriteAllText;

StreamWriter sw = new StreamWriter(myfilepath);
                sw.Write(mystring);
                sw.Close();
                sw.Dispose();

;

using (FileStream fstr = File.Create(myfilepath))
                {
                StreamWriter sw = new StreamWriter(myfilepath);
                sw.Write(mystring);
                sw.Close();
                sw.Dispose();
                fstr.Close();
                }

All I am trying to do is to access a file, write on it, then close it. I might be making a silly mistake but I would like to understand what I am doing wrong and why. How to make sure that the file is closed and not to cause this error again.

Helped by the answers so far I did this:

using (FileStream fstr = File.Open(myfilepath,FileMode.OpenOrCreate,FileAccess.ReadWrite))
                {
                    StreamWriter sw = new StreamWriter(fstr);
                    sw.Write(mystring);
                    sw.Close();


                }

It seems to be better because it seems to close/stop the process of my file if I try to access another file on the second time I access the page. But if I try to access the same file on a second time, it gives me the error again.

  • 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-08T17:36:10+00:00Added an answer on June 8, 2026 at 5:36 pm

    I would like to thank everyone for the help.
    In fact, apart from this code I found out that I had a stremReader still opened somewhere else after the code above. At the end I changed the code I had before for this:

    using (FileStream fstr = File.Open(myfile, FileMode.OpenOrCreate, FileAccess.ReadWrite))
    
                        {
                            StreamWriter sw = new StreamWriter(fstr);
                            sw.Write(mystring);
                            sw.Flush();
                            sw.Dispose();
                        }
    

    and on my StreamReader I did this:

    StreamReader sr = new StreamReader(myfile);
    string sometext = sr.ReadToEnd();
    sr.Dispose();
    

    I could also use this:

    File.ReadAllText(myfile);
    

    If there is something that I could have done in a better way please tell me.
    Thank you very much.

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

Sidebar

Related Questions

I'm getting this exception The process cannot access the file 'myfile.zip' because it is
I keep getting this error randomly: System.Web.Services.Protocols.SoapException: System.Web.Services.Protocols.SoapException: Server was unable to process request.
I keep getting this error and have no idea why. I googled and scanned
So I am getting this cross thread error and I cannot figure it out.
when upload the same file for the multiple times i am getting this error......
Hello I'm getting this error, while trying to save serial number to XML File.
Getting this error: 2009-09-03 12:44:02.307 xcodebuild[307:10b] warning: compiler 'com.apple.compilers.llvm.clang.1_0.analyzer' is based on missing compiler
Getting this error with jquery & jquery.form. Site has been live for awhile..upgraded to
Getting this error when try to add an item to my repositories/context: Collection has
Am getting this error message and matched my brackets and couldn't find anything wrong.

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.