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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:45:12+00:00 2026-06-09T21:45:12+00:00

I am having a problem with a pretty simply program. We started off with

  • 0

I am having a problem with a pretty simply program. We started off with this bit of working code:

RemoteFileInfo result = new RemoteFileInfo();

string filePath = System.IO.Path.Combine(ConfigurationManager.AppSettings["OmnitureSPDir"], request.FileName);
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

// check if exists
if (!fileInfo.Exists)
    throw new System.IO.FileNotFoundException("File not found",
                      request.FileName);

// open stream
System.IO.FileStream stream = new System.IO.FileStream(filePath,
          System.IO.FileMode.Open, System.IO.FileAccess.Read);

// return result 
result.FileName = request.FileName;
result.Length = fileInfo.Length;
result.FileByteStream = stream;

return result;

This takes in a parameter called “request” that contains the filename, we check to see if the file exists, and if so, we stream the file.

Well, after going through the microsoft code analysis in Visual Studio, and noticing that there was no dispose(), I “solved” the issue by wrapping it in a using statement:

using (RemoteFileInfo result = new RemoteFileInfo()) {

    string filePath = System.IO.Path.Combine(ConfigurationManager.AppSettings["OmnitureSPDir"], request.FileName);
    System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

    // check if exists
    if (!fileInfo.Exists)
        throw new System.IO.FileNotFoundException("File not found",
                          request.FileName);

    // open stream
    System.IO.FileStream stream = new System.IO.FileStream(filePath,
              System.IO.FileMode.Open, System.IO.FileAccess.Read);

    // return result 
    result.FileName = request.FileName;
    result.Length = fileInfo.Length;
    result.FileByteStream = stream;

    return result;
}

After running this code, we found that the file no longer would stream, but would error with:

Value cannot be null.
Parameter name: FileByteStream

Taking the Using() statement out, fixed the problem, but I don’t understand why. I thought I was doing a good thing, by adding in the code. I would like to understand what I did wrong, so that I don’t repeat it and can properly code this method.

Here is the definition for RemoteFileInfo

public class RemoteFileInfo : IDisposable
{
    [MessageHeader(MustUnderstand = true)]
    public string FileName;

    [MessageHeader(MustUnderstand = true)]
    public long Length;

    [MessageBodyMember(Order = 1)]
    public System.IO.Stream FileByteStream;

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);

    }

    protected virtual void Dispose(bool disposing)
    {
        if (disposing && FileByteStream != null)
            {
                FileByteStream.Close();
                FileByteStream = null;
            }

    } 
}
  • 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-09T21:45:13+00:00Added an answer on June 9, 2026 at 9:45 pm

    If you are returning an object that implements IDisposable, then you cannot Dispose it. That has to be up to the caller.

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

Sidebar

Related Questions

I'm pretty new to FPGA and Verilog, but I'm having a problem getting my
this should be pretty easy for ruby wizards here. I'm having a problem with
So I'm having this problem, it should be pretty simple, but I don't know
I'm having a bit of a problem finding out how to cancel this task
I'm having a problem with a pretty simple setup in NHibernate. (I'm using Fluent
I'm having a pretty big problem trying to create navigation on my page. If
Im having problem in my UIscrollView ,this is what I have done: Whenever a
I am having problem using mvc:resources in spring 3.1 configuration. Initially i was working
I am having problem to get a numerical value for this expression where I
I'm having problem with datagrid view. I have attached an image with the code

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.