770 snippets from 1556 members, and growing!
|
login
|
join
about
bytebin
members
tags
snippets
join
Snippets
Submit a Snippet
Search Snippets
New Snippets
Top Snippets
Top Tags
PHP
(136)
JavaScript
(123)
Java
(66)
VBSCRIPT
(58)
String
(44)
CSS
(31)
CSharp
(28)
File
(28)
HTML
(27)
C
(24)
mysql
(24)
VB.NET
(24)
python
(24)
CPlusPlus
(23)
groovy
(23)
New Snippets
Very lightweight ...
AutoComplete plug...
AutoComplete plug...
Connection Java -...
View PostgreSql
Store Procedure
Pygame - Simple p...
Python - Anagram ...
Python - Anagram ...
Converter to bina...
Venture Capital Jobs
New Members
me
jamesmcm
Can
Kelmi
ysg
dannymo2
chorny
wallie
Hackdemian
impomatic
Top Members
dannyboy
sundaramkumar
mattrmiller
Pio
i_kenneth
ASmith
ctiggerf
sehrgut
bertheymans
SCoon
Home
/
Snippets
/
Write a binary file
/
Comments
Write a binary file
Snippet Menu
Revisions
Comments
Related Snippets
Add to Favorites
Email Snippet
Download Snippet
Print Snippet
Blog Snippet
snippet
|
revisions
|
comments
|
related
|
Add to Favorites
|
email
download
|
print
|
blog it
New Comment
use using!
Mon. Aug. 28th, 2006 9:34 AM
bobbyrward
The second close in your example is redundant. BinaryWriter.Close closes the underlying stream(in this case, fs).
A better example...
Bobby R Ward
---------------------
bobbyrward@gmail.com
/// <summary>
/// Write binary to a stream
/// </summary>
/// <param name="stream">The System.IO.Stream based object to write to</param>
public
void
WriteBinaryStream
(
System
.
IO
.
Stream
stream,
byte
[
]
contents
)
{
using
(
System
.
IO
.
BinaryWriter
w =
new
System
.
IO
.
BinaryWriter
(
stream
)
)
w.
Write
(
contents
)
;
}
/// <summary>
/// Write binary to a file
/// </summary>
/// <param name="stream">The System.IO.Stream based object to write to</param>
public
void
WriteBinaryFile
(
string
filename,
byte
[
]
contents
)
{
using
(
System
.
IO
.
FileStream
fs =
new
System
.
IO
.
FileStream
(
filename,
System
.
IO
.
FileMode
.
Create
)
)
WriteBinaryStream
(
fs, contents
)
;
}
Reply
New Comment
A better example...
Bobby R Ward
---------------------
bobbyrward@gmail.com
/// <summary>
/// Write binary to a stream
/// </summary>
/// <param name="stream">The System.IO.Stream based object to write to</param>
public void WriteBinaryStream(System.IO.Stream stream, byte[] contents)
{
using(System.IO.BinaryWriter w = new System.IO.BinaryWriter(stream))
w.Write(contents);
}
/// <summary>
/// Write binary to a file
/// </summary>
/// <param name="stream">The System.IO.Stream based object to write to</param>
public void WriteBinaryFile(string filename, byte[] contents)
{
using(System.IO.FileStream fs = new System.IO.FileStream(filename, System.IO.FileMode.Create))
WriteBinaryStream(fs, contents);
}