read binary file into byte[]

What is the most efficient technique to read a binary file ( such as a .dll 
or .exe )  into a byte[] ?

Are there advantages to using a BinaryReader ?

Thanks. 


0
John
11/13/2009 11:37:58 PM
dotnet.languages.csharp 1931 articles. 0 followers. Follow

7 Replies
1383 Views

Similar Articles

[PageSpeed] 5

John Grandy wrote:
> What is the most efficient technique to read a binary file ( such as a .dll 
> or .exe )  into a byte[] ?
> 
> Are there advantages to using a BinaryReader ?

BinaryReader is mainly useful for when you are dealing with specific 
data types _other_ than an array of bytes.

As for "the most efficient technique" for reading a binary file, it all 
depends.  But, depending on your needs, your first try should be simply 
to allocate a byte[] of the necessary size, and then make a single call 
to FileStream.Read() to read all of the data at once.

For absolute correctness, you should actually code that as a loop, 
checking the return value of Read() and calling it again to read any 
remaining bytes if the return value is less than the total length 
remaining to read.

But for a FileStream, the complete file _should_ be always read in a 
single call to Read().

Doing it this way delegates all of the buffering and data copying tasks 
to .NET and Windows.  Most of the time, those components will be able to 
do the i/o as efficiently as possible.  If you have some specific need 
or consideration that requires a different strategy, you should be 
specific about that so you can receive advice specific to that need or 
consideration.

Finally, note that with respect to file i/o, unless you are accessing 
the same file repeatedly, it's unlikely that anything you do in your 
code will very much affect the over throughput, because the real 
bottleneck is simply in getting the data from the storage device.

Pete
0
Peter
11/13/2009 11:48:08 PM
Hi Peter, and thanks for the response.

So, the techniques in the following article :

http://www.yoda.arachsys.com/csharp/readbinary.html

are generally not necessary ?  What special cases would require looping ?


"Peter Duniho" <no.peted.spam@no.nwlink.spam.com> wrote in message 
news:OyViBvLZKHA.1652@TK2MSFTNGP05.phx.gbl...
> John Grandy wrote:
>> What is the most efficient technique to read a binary file ( such as a 
>> .dll or .exe )  into a byte[] ?
>>
>> Are there advantages to using a BinaryReader ?
>
> BinaryReader is mainly useful for when you are dealing with specific data 
> types _other_ than an array of bytes.
>
> As for "the most efficient technique" for reading a binary file, it all 
> depends.  But, depending on your needs, your first try should be simply to 
> allocate a byte[] of the necessary size, and then make a single call to 
> FileStream.Read() to read all of the data at once.
>
> For absolute correctness, you should actually code that as a loop, 
> checking the return value of Read() and calling it again to read any 
> remaining bytes if the return value is less than the total length 
> remaining to read.
>
> But for a FileStream, the complete file _should_ be always read in a 
> single call to Read().
>
> Doing it this way delegates all of the buffering and data copying tasks to 
> .NET and Windows.  Most of the time, those components will be able to do 
> the i/o as efficiently as possible.  If you have some specific need or 
> consideration that requires a different strategy, you should be specific 
> about that so you can receive advice specific to that need or 
> consideration.
>
> Finally, note that with respect to file i/o, unless you are accessing the 
> same file repeatedly, it's unlikely that anything you do in your code will 
> very much affect the over throughput, because the real bottleneck is 
> simply in getting the data from the storage device.
>
> Pete 


0
John
11/14/2009 12:01:30 AM
John Grandy wrote:
> Hi Peter, and thanks for the response.
> 
> So, the techniques in the following article :
> 
> http://www.yoda.arachsys.com/csharp/readbinary.html
> 
> are generally not necessary ?  What special cases would require looping ?

As I wrote in my previous reply, you _should_ loop.  Technically, the 
Read() method is only required to block as long as it takes to get 
_some_ data.  This leaves open the possibility that it might return 
before having read _all_ of the data.

In reality, the Read() method generally only returns before 
end-of-stream and/or filling the buffer if for some reason the stream 
has a way of indicating data isn't yet available, and that becomes true 
after reading some data already.  And in reality, that's just not 
something that typically happens with FileStream.

But, the API doesn't provide any _guarantee_ of that.  That's an 
implementation detail in the API that you shouldn't count on.  Thus my 
comment about "for absolute correctness".

In your case, the first non-"Bad code" block Jon shows should suffice.

Pete
0
Peter
11/14/2009 2:23:47 AM
"John Grandy" <johnagrandy-at-gmail-dot-com> wrote in message 
news:u6MuWpLZKHA.5300@TK2MSFTNGP02.phx.gbl...
> What is the most efficient technique to read a binary file ( such as a 
> .dll or .exe )  into a byte[] ?

    If we interpret "most efficient" as "the one that requires me to write 
the least amount of code", then it's probably File.ReadAllBytes:

using System.IO;
....
byte[] theBytes = File.ReadAllBytes(pathname);


0
Alberto
11/14/2009 9:50:58 AM
John Grandy wrote:
> Hi Peter, and thanks for the response.
> 
> So, the techniques in the following article :
> 
> http://www.yoda.arachsys.com/csharp/readbinary.html
> 
> are generally not necessary ?  What special cases would require looping ?
> 

For two reasons:

1. There is no guarantee that it works without looping. You could dig 
deep in the actual implementation to find out if it is ever needed when 
reading from a file or not, but you shouldn't rely on uncodumented 
implementation details like that.

2. Code spreads. Even if it works for reading a stream that is a local 
file, it won't work for a stream that is slower, like a web response. 
When you copy your code that works fine in one project into another 
project, it will fail.

Besides, the File.ReadAllBytes method already has the code, correctly 
implemented.

-- 
G�ran Andersson
_____
http://www.guffa.com
0
ISO
11/15/2009 2:29:44 AM
John Grandy wrote:
> What is the most efficient technique to read a binary file ( such as a .dll 
> or .exe )  into a byte[] ?
> 
> Are there advantages to using a BinaryReader ?
> 
> Thanks. 
> 
> 

If I don't remember wrong, W. Richard Stevens et al compared different 
ways to copy a file to another in "Advanced Programming in the UNIX 
Environment". Memory mapped files turn out to be fastest way. Ok, it was 
both reading and writing, so memory mapped files might no be fastest in 
reading. But bigger catch is, memory mapped files are not in .NET until 
version 4.0.

--
Arto Viitanen
0
Arto
11/16/2009 6:25:27 AM
Arto Viitanen wrote:
> John Grandy wrote:
>> What is the most efficient technique to read a binary file ( such as a 
>> .dll or .exe )  into a byte[] ?
>>
>> Are there advantages to using a BinaryReader ?
>>
>> Thanks.
>>
> 
> If I don't remember wrong, W. Richard Stevens et al compared different 
> ways to copy a file to another in "Advanced Programming in the UNIX 
> Environment". Memory mapped files turn out to be fastest way. Ok, it was 
> both reading and writing, so memory mapped files might no be fastest in 
> reading. But bigger catch is, memory mapped files are not in .NET until 
> version 4.0.

I don't understand this response.  Did "W. Richard Stevens et al" do 
their comparison using .NET code?  In a "...UNIX Environment" book?

If not, then their analysis isn't really that applicable to this question.

A determined person can use memory mapped files in .NET pre-4.0 (which 
is still only in beta), by using p/invoke to access the unmanaged API 
for memory mapped files.

I doubt for reading data into an array it would be all that worthwhile 
though.  I have seen in my own programs that for files that are read 
just once, the bottleneck is the disk (subsequent reads of the same data 
can go much faster though...10-20 times faster, or even more I've seen 
in some cases, due to caching).

Pete
0
Peter
11/16/2009 8:31:05 AM
Reply:

Similar Artilces:

File Not Found msg when run Excel
What would cause a "file not found" error upon starting Excel (w/o any reference to a file to be loaded on startup). I've checked in the VBA screen and can find no macros set to fire on startup. You can load workbooks each time when excel starts via the Tools|Addins dialog. If you've moved/deleted one of those files, excel will yell at you that it can't find it. If you start excel (ignore the message) and then tools|addins you can uncheck the selected addins. When you uncheck the missing addin, excel will notice that it can't find it and ask if you want to remo...

Problem template file for project missing
Hi, I try to create a new UnitTest project in Visual Studio 2008 Development Edition. I get the message: "The template file for selected project type is missing". Well, I tried to repair Visual Studio but the message is still comes. Any idea how I can solve this problem? Thanks Christian I solved the problem. I had installed a language pack for Visual Studio Tools. Well, Visual Studio searched in the directory "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\1031" and not the subdirectory "1033". I deinstalled ...

Last Updated Filed
What event triggers the update of the field LastUpdated in the Items table? Marc This is a multi-part message in MIME format. ------=_NextPart_000_0280_01C7F013.BD3F8510 Content-Type: multipart/alternative; boundary="----=_NextPart_001_0281_01C7F013.BD3F8510" ------=_NextPart_001_0281_01C7F013.BD3F8510 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Marc, Depends on what version you have as its been fixed and broken many times = over the last year!=20 Look it up on CustomerSource hotfix list. --=20 Jeff=20 Check Poin...

Changing the default folder when inserting files.
Does anybody know how to change the default folder Outlook opens when inserting files? I have already tried the registry with no luck. Thanks. you can change the default folder - use the places bar to select the folder you want. http://www.poremsky.com/p/places_bar.htm -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Coauthor, OneNote 2003 for Windows (Visual QuickStart Guide) Outlook Tips: http://www.outlook-tips.net/ Outlook & Exchange Solutions Center: http://www.slipstick.com "Kristin" <anonymous@discussions.microsoft.com> ...

File Modified by Another User #2
I have recently upgraded my PC to Windows XP SP2 and am having a curious problem. When I attempt to save the file, I get a dialog box stating the file might have been modified by another user - do I wish to save a copy or overwrite changes. The workbook is not shared - however, it does reside on a Linux Samba share. But then again, it has always resided on the share. Only since the SP2 update, has this started happening: These are the steps I followed: 1. Opened Excel 2. Opened the file on the Samba share 3. Clicked SAVE 4. Got the dialog box asking to overwrite changes or s...

2003: Page Setup missing from File menu
Is this by design or have I done something strange? I want to freeze the top line to repeat on all sheets and cannot find anywhere else to do that. tcebob File>Page Setup is gone from your menu? It should be there. Are you missing anything else? Try putting Page Setup back onto the File Menu. Tools>Customize>Commands>File Scroll down to "Page Setup" and left-click drag to File Menu. If you have other things missing from your menus maybe you should just try re-setting all. Tools>Customize>Toolbars. Select Worksheet Menu bar and &q...

interpreter .csv files (when opening .csv) should be configurable
Whenever I open a .csv file that is ; delimited by doubleclicking it in windows explorer, excel automatically interprets it as comma delimited. I'd like to be able to either turn off the automatic interpreter (f.ex. by holding the CTRL button when opening a file) or change the default interpreting settings. ---------------- This post is a suggestion for Microsoft, and Microsoft responds to the suggestions with the most votes. To vote for this suggestion, click the "I Agree" button in the message pane. If you do not see the button, follow this link to open the su...

Saving file for new year
I would like to redo my Money file for the new year. I have transactions back to 2003. I'd like to create a new file that begins with 2008. I remember years ago that Quicken gave me that option. Is there anyway to do this with Micorsoft Money? In microsoft.public.money, grsode wrote: >I would like to redo my Money file for the new year. I have transactions >back to 2003. I'd like to create a new file that begins with 2008. I >remember years ago that Quicken gave me that option. Is there anyway to do >this with Micorsoft Money? File->New->NewFile lets ...

Can't open my money file #2
Good evening, From today i can't open my money file, i had formatted my pc and i reinstalled money as usual. Today when i open my money file after typing the correct password the program show a screen where it say "Money has detected that you did not close file xxx before exiting Money now needs to verify the information in your file. .... Do you want do this now?" I click yes and it say "The email address or password is incorrect. Please try again" But i dont put nowhere any email address this is an old money file converted to new money 2008 un year ago i never ...

transferring files [inbox, sent mail, etc] from outlook
Recently installed Windows 7 and began using windows live mail. Previously had windows xp and windows outlook. I am trying to transfer the mail files [inbox, sent, drafts, etc] to windows live mail but cannot figure out how to do that. Help! If you really mean Outlook, it's more complicated. You need to take the ..pst file to a computer running Outlook, import the messages into Outlook Express first. If you mean Outlook Express, then you need to copy all of the .dbx files to the Win 7 computer, then import them into WLM. File - Import - Messages - Microsoft Outlook Ex...

mporting multiple text files into Excel with corresponding filenames
Dear members, I need to import 100+ text files into Excel. In addition I want Excel to show corresponding filename (where the data comes from) in each row. How do I accomplish this? TIA Hi Take a look at Ron de Bruin's site. He has lots of code examples to consolidate files. http://www.rondebruin.nl/tips.htm -- Regards Roger Govier "avi" <avi38521@gmail.com> wrote in message news:9194d7dc-21bb-4fd9-8ecb-aa301a54f7d1@b2g2000yqi.googlegroups.com... > Dear members, > > I need to import 100+ text files into Excel. > > In addition ...

Money 2001 Crash, file verify on restart fails/locks
On reading the unnofficial FAQs for the newsgroup through a websearch for a solution to my problem, it sounds like I may have no hope, but didn't address my specific problem so I thought I'd see if anyone had a clue as to what I should do. Upon opening M2001 last night, it crashed and said (I believe) msmoney.exe must shut down as it experienced a fatal error (I was only trying to open the program) it has done it a few times before, but upon reopening the program it will open my money file and work fine. This time, when I reopened it told me that I had not properly closed the file th...

saving and remaming files
Folks, Have a small company with 5 pc's networked with Server 2000. Recently users have complained that when they open excel files (that are stored on the server and shared by all users) and make changes...they hit "save" (as they always have) and it renames the file as (if the secretary has recalled the error message correctly) as a temp file. This has obviously caused problems with the other users. When they go to open the folder (after changes have been made by another user) they do not see the changes. They have combated the problem even further as they have started to rena...

How can I detach an audio file from a PowerPoint Presentation?
Is there a way to do this? I'm sure I could use an audio capture utility to do this, but I would prefer to obtain the audio in it's original format. Please advise. Thanks in advance for your time and help. Save As > Web Page (*.html) The sound file will be in the folder of dependant files -- john ATSIGN PPTAlchemy.co.uk Free PPT Hints, Tips and Tutorials http://www.pptalchemy.co.uk/powerpoint_hints_and_tips_tutorials.html "Titanium" wrote: > Is there a way to do this? > > I'm sure I could use an audio capture utility...

location of outlook.pst file
I regularly want to make a copy of my outlook.pst file and bring it to another location. Right now the file location buried in a sub-sub directory \Documents and Settings\[name]\local settings\application data, which I think is the default setting. Sometimes I forget the exact location or have to get to it in explorer to copy it to a flash drive and them transport it. Is there any reason I could not move it to say: C:\outlook data? Are there any disadvantages in doing so? No, moving it is something I would recommend. However I would really recommend against creating subfolders of your...

renaming files
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel in word I could rename a file from the open menu so long as the file was not launched. what is the most efficient way to rename a file in word for mac? mtevans14@officeformac.com wrote: > Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: > Intel in word I could rename a file from the open menu so long as the > file was not launched. what is the most efficient way to rename a file > in word for mac? Hi, Windows OS lets you change names in File Open dialog boxes. MacO...

save font with file
I have a font for GDT symbols installed on my PC, there is no problem to display and print this document from my PC, but when I try on another PC which do not have installed the GDT font, this is missed. how can I save the font with the excel file? Hi, I'm not an expert in this area, but I believe you need to have the fonts installed on each pc that needs it; a good reason to keep to familiar fonts, I guess. jeff >-----Original Message----- >I have a font for GDT symbols installed on my PC, there >is no problem to display and print this document from my >PC, but whe...

PUB file crashes Publisher on open
I have a publisher file that crashes MS Publisher on open, even tried some recovery programs and they crashed. Is it a lost cause? How to troubleshoot a damaged publication in Publisher http://support.microsoft.com/kb/198256/en-us -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com http://officebeta.iponet.net/en-us/publisher/FX100649111033.aspx <dmahlitz@gmail.com> wrote in message news:1161174335.508250.183640@m73g2000cwd.googlegroups.com... >I have a publisher file that crashes MS Publisher on open, even tried > some rec...

Excel: Open Recent Files
Version: 2008 Operating System: Mac OS X 10.6 (Snow Leopard) Processor: Intel I can't get the list to change. It continues to list the same 10 files all the time. <br><br>If I delete the Microsoft Preferences file, i.e. com.microsoft.Excel.plist, the next time I start up Excel, the same 10 files are there. <br><br>If I open several new files, they initially get added to the list. However, once again, once I re-start Excel, the original 10 files are still there. <br><br>If I turn off the recently opened list in the Excel General preferences, delete ...

copy a file to all users via a script instead of manually
I have a d:\users folder on my server. inside users, each user has a folder named with their name. inside each of those folders is a desktop folder. I have a file on the C drive that I need to copy to each of those desktop folders, thereby giving each user this shortcut. Does anyone have a script that could do that, instead of me copying this file manually a few hundred times? many thanks if you do! "Rich" <richjone@noemail.noemail> wrote in message news:5E68A580-FA4D-4B73-A548-0ED9D74AF34C@microsoft.com... > I have a d:\users folder on my server. inside...

corrupt file
I made changes to an 07 project file. I saved it in 03. When I went to X out it asked to save it again. I saved again. Then when I went to X out it asked me again. I just got out w/o saving. I had saved the file fairly recently. Now when I am trying to open the file it says corrupt content and wont' open. Is there anything I can do? This is a multi-part message in MIME format. ------=_NextPart_000_0011_01CADE4C.B8604C80 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: quoted-printable Hi Penny, Welcome to this Microsoft Pr...

overwritten excel file
Is there any way to open and excell file and retrieve an earlier version at a earliet priouc date even thoug it was saved/over-written to the same file name / same location? Dan, No, the original version is gone forever. -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting, LLC www.cpearson.com "Dan LG" <Dan LG@discussions.microsoft.com> wrote in message news:43E2B30B-B983-4F85-9DCF-6EE0298B6449@microsoft.com... > Is there any way to open and excell file and retrieve an earlier version at a earliet priouc date even thoug it was saved/over-wri...

File Permissions
I'm trying to setup file permissions on a single file shared on the server, whereby the Admisitrator has full control, but certain users have 'Read' only permsision. I want users to be able to access the file and read the contents, but no be able to modify or change the contents. Simple enough, but within the file's Security tab I have the Administrator, and Users group added (nothing else), and permissions are set to Full control for Administrator, and Read & Execute, and Read for the Users. However, the users are unable to open the file (Access Denied), unles...

Word 2010
Hi, (posting this here as well as in the 2010 beta forum - I know from experience that it is here I get the answers :-)) Are there any new possibilities to structure your templates in the File New dialog? The need is obvious when you have several hundreds of differnet templates. One good solution would be that an underlying folder structure shows in the dialog. (Like the first level in the old fileNew dialog) Another question is that even though you under Options, Advanced, File Locations set a location for "Workgroup Templates", the templates are not shown...

Exporting e-mail to a data file
I've tried a couple of varieties (Access, CSV) with similar results. Neither option includes the "Received" field. Why is that? How do I include the "Received" field in the exported file? Thank you, Bill Allen If you use Access to set up a linked table connected to your mail = folder, you will see the Received date among the available properties. = Third-party export tools are also available; see = http://www.outlookcode.com/d/customimport.htm#tools --=20 Sue Mosher, Outlook MVP Author of Configuring Microsoft Outlook 2003 http://www.turtleflock.com/olco...