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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:07:27+00:00 2026-06-09T05:07:27+00:00

I’m trying to convert a .db file to binary so I can stream it

  • 0

I’m trying to convert a .db file to binary so I can stream it across a web server. I’m pretty new to C#. I’ve gotten as far as looking at code snippets online but I’m not really sure if the code below puts me on the right track. How I can write the data once I read it? Does BinaryReader automatically open up and read the entire file so I can then just write it out in binary format?

class Program
{
    static void Main(string[] args)
    {
        using (FileStream fs = new FileStream("output.bin", FileMode.Create))
        {
            using (BinaryWriter bw = new BinaryWriter(fs))
            {
                long totalBytes = new System.IO.FileInfo("input.db").Length;
                byte[] buffer = null;

                BinaryReader binReader = new BinaryReader(File.Open("input.db", FileMode.Open)); 
            }
        }
    }
}

Edit: Code to stream the database:

[WebGet(UriTemplate = "GetDatabase/{databaseName}")]
public Stream GetDatabase(string databaseName)
{
    string fileName = "\\\\computer\\" + databaseName + ".db";

    if (File.Exists(fileName))
    {
        FileStream stream = File.OpenRead(fileName);

        if (WebOperationContext.Current != null)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "binary/.bin";
        }

        return stream;
    }

    return null;
}

When I call my server, I get nothing back. When I use this same type of method for a content-type of image/.png, it works fine.

  • 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-09T05:07:28+00:00Added an answer on June 9, 2026 at 5:07 am

    All the code you posted will actually do is copy the file input.db to the file output.bin. You could accomplish the same using File.Copy.

    BinaryReader will just read in all of the bytes of the file. It is a suitable start to streaming the bytes to an output stream that expects binary data.

    Once you have the bytes corresponding to your file, you can write them to the web server’s response like this:

    using (BinaryReader binReader = new BinaryReader(File.Open("input.db", 
                                                     FileMode.Open))) 
    {
        byte[] bytes = binReader.ReadBytes(int.MaxValue); // See note below
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.Close();
        Response.End();
    }
    

    Note: The code binReader.ReadBytes(int.MaxValue) is for demonstrating the concept only. Don’t use it in production code as loading a large file can quickly lead to an OutOfMemoryException. Instead, you should read in the file in chunks, writing to the response stream in chunks.

    See this answer for guidance on how to do that

    https://stackoverflow.com/a/8613300/141172

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.