By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,960 Members | 2,246 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,960 IT Pros & Developers. It's quick & easy.

Read,write a file binary

P: n/a
How do I read, write a file binary?
I want to open, say, file.exe read it in to the program, then write it out
to file2.exe.
Like file copy, anyone have a code sample?
Nov 15 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Patrick,

File.Copy.

More general: BinaryReader/BinaryWriter.
Best regards,

Henrik Dahl

"Patrik Malmström" <po*****@popsork.com> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
How do I read, write a file binary?
I want to open, say, file.exe read it in to the program, then write it out
to file2.exe.
Like file copy, anyone have a code sample?

Nov 15 '05 #2

P: n/a
I've looked at them, but the exampel, wish I always use aren't very good
"Henrik Dahl" <Th**********************@inet.uni2.dk> skrev i meddelandet
news:uA**************@TK2MSFTNGP11.phx.gbl...
Patrick,

File.Copy.

More general: BinaryReader/BinaryWriter.
Best regards,

Henrik Dahl

"Patrik Malmström" <po*****@popsork.com> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
How do I read, write a file binary?
I want to open, say, file.exe read it in to the program, then write it out to file2.exe.
Like file copy, anyone have a code sample?


Nov 15 '05 #3

P: n/a
Here ya go,

You will need to clean that up a little bit, but its a works...

-- Read

FileStream fs = new FileStream(@"C:\calc.exe", FileMode.OpenOrCreate,
FileAccess.Read);
byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();

-- Write

FileStream fs1 = new FileStream(@"C:\calc1.exe", FileMode.OpenOrCreate,
FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs1);
bw.Write(MyData);
bw.Close();
fs1.Close();

"Patrik Malmström" <po*****@popsork.com> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
I've looked at them, but the exampel, wish I always use aren't very good
"Henrik Dahl" <Th**********************@inet.uni2.dk> skrev i meddelandet
news:uA**************@TK2MSFTNGP11.phx.gbl...
Patrick,

File.Copy.

More general: BinaryReader/BinaryWriter.
Best regards,

Henrik Dahl

"Patrik Malmström" <po*****@popsork.com> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
How do I read, write a file binary?
I want to open, say, file.exe read it in to the program, then write it out to file2.exe.
Like file copy, anyone have a code sample?



Nov 15 '05 #4

P: n/a
Pramod,

Looks OK, but it could benefit from e.g. the following:

*) Use a BinaryReader for reading.
*) use using instead of instantiate ... Close. In this case disposing object
also work in case an exception should be thrown plus it's much more
declarative by nature.
*) Use (int) instead of System.Convert.ToInt32.

Additionally it may be advantageous to use the BitConverter class.
Best regards,

Henrik Dahl

"Pramod Anchuparayil" <pr**********@hotmail.com> wrote in message
news:e4***************@TK2MSFTNGP10.phx.gbl...
Here ya go,

You will need to clean that up a little bit, but its a works...

-- Read

FileStream fs = new FileStream(@"C:\calc.exe", FileMode.OpenOrCreate,
FileAccess.Read);
byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();

-- Write

FileStream fs1 = new FileStream(@"C:\calc1.exe", FileMode.OpenOrCreate,
FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs1);
bw.Write(MyData);
bw.Close();
fs1.Close();

"Patrik Malmström" <po*****@popsork.com> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
I've looked at them, but the exampel, wish I always use aren't very good
"Henrik Dahl" <Th**********************@inet.uni2.dk> skrev i meddelandet news:uA**************@TK2MSFTNGP11.phx.gbl...
Patrick,

File.Copy.

More general: BinaryReader/BinaryWriter.
Best regards,

Henrik Dahl

"Patrik Malmström" <po*****@popsork.com> wrote in message
news:uB**************@TK2MSFTNGP12.phx.gbl...
> How do I read, write a file binary?
> I want to open, say, file.exe read it in to the program, then write
it out
> to file2.exe.
> Like file copy, anyone have a code sample?
>
>



Nov 15 '05 #5

P: n/a
Henrik Dahl <Th**********************@inet.uni2.dk> wrote:
Looks OK, but it could benefit from e.g. the following:

*) Use a BinaryReader for reading.
What's the benefit there? I believe BinaryReader and BinaryWriter are
really there to make it easier to read and write primitives, not blocks
at a time.
*) use using instead of instantiate ... Close. In this case disposing object
also work in case an exception should be thrown plus it's much more
declarative by nature.
*) Use (int) instead of System.Convert.ToInt32.

Additionally it may be advantageous to use the BitConverter class.


Why?

Actually, I'd write it something like:

using (FileStream input = new FileStream (...))
{
using (FileStream output = new FileStream (...))
{
byte[] buffer = new byte[16384];
int len;
while ( (len=input.Read (buffer, 0, buffer.Length)) > 0)
{
output.Write (buffer, 0, len);
}
}
}

That way you don't need to read the whole lot into memory at a time.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #6

P: n/a
Thanks everyone!
Nov 15 '05 #7

P: n/a

"Jon Skeet" <sk***@pobox.com> wrote in message
news:MP************************@news.microsoft.com ...
Henrik Dahl <Th**********************@inet.uni2.dk> wrote:
Looks OK, but it could benefit from e.g. the following:

*) Use a BinaryReader for reading.
What's the benefit there? I believe BinaryReader and BinaryWriter are
really there to make it easier to read and write primitives, not blocks
at a time.


Yes, but often people have some order in their bytes where it's possible to
take advantage of these services.
*) use using instead of instantiate ... Close. In this case disposing object also work in case an exception should be thrown plus it's much more
declarative by nature.
*) Use (int) instead of System.Convert.ToInt32.

Additionally it may be advantageous to use the BitConverter class.
Why?


Same argument as before plus if the current architecture lets him to do it
at a higher abstration level.

Actually, I'd write it something like:

using (FileStream input = new FileStream (...))
{
using (FileStream output = new FileStream (...))
{
byte[] buffer = new byte[16384];
int len;
while ( (len=input.Read (buffer, 0, buffer.Length)) > 0)
{
output.Write (buffer, 0, len);
}
}
}

That way you don't need to read the whole lot into memory at a time.
I agree in your comment, but if this is the real thing to achieve I would
just use File.Copy. If the point just is to do the copying there's no point
in neither BinaryReader or BitConverter of course.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Nov 15 '05 #8

P: n/a
Patrik,

Have you installed the .NET framework on the machine where you got this
error message?
Best regards,

Henrik Dahl

"Patrik Malmström" <po*****@popsork.com> wrote in message
news:eq**************@TK2MSFTNGP12.phx.gbl...
I'll tried that code, ´but when I was trying to start calc1.exe, it sayed
that calc1.exe doesn't are a Win32 program, sorrym but I'm no good at C#.
"Pramod Anchuparayil" <pr**********@hotmail.com> skrev i meddelandet
news:e4*************@TK2MSFTNGP10.phx.gbl...
Here ya go,

You will need to clean that up a little bit, but its a works...

-- Read

FileStream fs = new FileStream(@"C:\calc.exe", FileMode.OpenOrCreate,
FileAccess.Read);
byte[] MyData= new byte[fs.Length];
fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));
fs.Close();

-- Write

FileStream fs1 = new FileStream(@"C:\calc1.exe", FileMode.OpenOrCreate,
FileAccess.Write);
BinaryWriter bw = new BinaryWriter(fs1);
bw.Write(MyData);
bw.Close();
fs1.Close();

"Patrik Malmström" <po*****@popsork.com> wrote in message
news:ej**************@TK2MSFTNGP10.phx.gbl...
I've looked at them, but the exampel, wish I always use aren't very good "Henrik Dahl" <Th**********************@inet.uni2.dk> skrev i meddelandet news:uA**************@TK2MSFTNGP11.phx.gbl...
> Patrick,
>
> File.Copy.
>
> More general: BinaryReader/BinaryWriter.
>
>
> Best regards,
>
> Henrik Dahl
>
> "Patrik Malmström" <po*****@popsork.com> wrote in message
> news:uB**************@TK2MSFTNGP12.phx.gbl...
> > How do I read, write a file binary?
> > I want to open, say, file.exe read it in to the program, then
write
it out
> > to file2.exe.
> > Like file copy, anyone have a code sample?
> >
> >
>
>



Nov 15 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.