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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:50:41+00:00 2026-05-14T00:50:41+00:00

I have this code, I open a stream (without closing or disposing it), then

  • 0

I have this code, I open a stream (without closing or disposing it), then I copy the file used by the stream, I thought I should get a “Process can’t access file somefile.xml because it’s being used by another process” exception, but I don’t. Any idea why? Thanks! The code:

StreamWriter writer = new StreamWriter("C:\\somefile.xml");

writer.Write(string.Empty);

// I thought this should crash since a stream is using "C:\somefile.xml"
File.Copy("C:\\somefile.xml", "C:\\copy of somefile.xml", true);
  • 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-14T00:50:42+00:00Added an answer on May 14, 2026 at 12:50 am

    It’s because StreamWriter uses FileStream object and the default FileShare on the FileStream contructor which doesn’t specify a explicit fileshare is FileShare.Read.

    Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, even if this flag is specified, additional permissions might still be needed to access the file.

    You can override that by using FileStream constructor that allows specifying the FileShare then passing that to the StreamWriter constructor.

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

Sidebar

Related Questions

I have the following code to open Excel template file and save it as
I have created a WCF service to stream files(download). Code for the service is
HI. i am trying to open a local file from xml created by xlink
Note: I am not sure if this is better here, or at superuser, but
I am trying to use URLLoader to load an XML file from server (same
I am trying to get some profile information in my Facebook via PHP as
I have ifstream and an ofstream that in runtime might be opened or not
I've been writing and reading PDF's to a SQL Server 2008 FileStream for a
I just wrote a very simple Expect script for wrapping around rsync, but it
I leave the below in case anybody wants the record types, but my question

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.