BlueMonkMN
BlueMonkMN

Reputation: 25601

Most appropriate usage of stream returned by App.GetResourceStream

My question has already kind of been asked at Releasing underlying Stream returned by Application.GetResourceStream on WP7, but I have a couple variations on the question:

  1. This is for a desktop WPF application, not WP7 if it makes any difference.
  2. I have to decide whether to use a using block on a stream reader built on top of the stream.

Here's some code:

System.Windows.Resources.StreamResourceInfo ri = 
   App.GetResourceStream(new Uri("Resources/Idioms.txt", UriKind.Relative));
using (System.IO.StreamReader sr = new System.IO.StreamReader(ri.Stream))
{
   idioms = sr.ReadToEnd().Split(lineSeps, StringSplitOptions.RemoveEmptyEntries);
}

Now StreamReader's close method (which I assume is the method that implements IDisposable.Dispose) indicates that it also closes the underlying stream (which I assume is also implementing IDisposable.Dispose).

So is this closing or disposing of the IO.Stream object provided by GetResourceStream's Stream property:

  1. Expected
  2. Acceptable, or
  3. Incorrect

(Should I use or avoid using on the reader built on top of the stream?)

Upvotes: 0

Views: 192

Answers (1)

Sam Axe
Sam Axe

Reputation: 33738

StreamReader's ctor has an overload that lets you tell it to keep the stream from being closed.

Also, you have it slightly backwards (unless I misunderstood). It's the Dispose method that calls Close, not the other way around.

Upvotes: 1

Related Questions