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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:36:44+00:00 2026-05-15T08:36:44+00:00

This seems like a fairly straightforward question, but I couldn’t find this particular use-case

  • 0

This seems like a fairly straightforward question, but I couldn’t find this particular use-case after some searching around.

Suppose I have a simple method that, say, determines if a file is opened by some process. I can do this (not 100% correctly, but fairly well) with this:

public bool IsOpen(string fileName)
{
  try
  {
    File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None);
  }
  catch
  {
    // if an exception is thrown, the file must be opened by some other process
    return true;
  } 
}

(obviously this isn’t the best or even correct way to determine this – File.Open throws a number of different exceptions, all with different meanings, but it works for this example)

Now the File.Open call returns a FileStream, and FileStream implements IDisposable. Normally we’d want to wrap the usage of any FileStream instantiations in a using block to make sure they’re disposed of properly. But what happens in the case where we don’t actually assign the return value to anything? Is it still necessary to dispose of the FileStream, like so:

try
{
  using (File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None));
  { /* nop */ }
}
catch 
{
  return true;
}

Should I create a FileStream instance and dispose of that?

try
{
  using (FileStream fs = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None));
}
...

Or are these totally unnecessary? Can we simply call File.Open and not assign it to anything (first code example), and let the GC dispose of it right away?

  • 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-15T08:36:45+00:00Added an answer on May 15, 2026 at 8:36 am

    Yes, you should definitely dispose of the FileStream. Otherwise the stream will remain open and the file won’t be usable until a finalizer happens to clean it up.

    The important thing here is ownership: for File.Open, the caller is assumed to “own” the stream returned to it – and if you own something which implements IDisposable, it’s your responsibility to dispose of it.

    Compare this with the situation of Image.FromStream: in that case, you pass in a stream and the Image then assumes that it owns that stream. You mustn’t close the stream yourself, in that case – you have to dispose of the image when you’re done, and it will dispose of the stream.

    Calling a static method which returns something disposable almost always assumes that the caller takes ownership of the resource. Ditto constructors (which are effectively static methods.)

    The irony in this case is that if you don’t dispose of the stream returned by File.Open, you’ll have found that the file is usable – at the same time as making it unusable until some indeterminate time.

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

Sidebar

Related Questions

This seems like a fairly straightforward question but I've been unable to find an
This seems like a simple question, but I can't find it with the Stack
This seems like a pretty softball question, but I always have a hard time
This seems like a silly question but I would really like your comments and
This seems like perhaps a naive question, but I got into a discussion with
It seems like this should be straightforward but I'm boggling. I've got my listview
This seems like it should be fairly straight-forward, but I can't see anything obvious.
This should seem to be a fairly straightforward question, but I'm struggling a bit.
Firstly, this seems like (from ContourPlot) a fairly straightforward maximization problem, why is FindMaximum
This is probably fairly straightforward but i can't seem to find a reasonable explanation

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.