Вы находитесь на странице: 1из 2

How can I convert any file to binary mode in c#?

[closed]
up vote
-2
down vote
favorite
How can I convert any file like .exe or .doc .....etc to binary mode in c#
c#
share|edit
asked Mar 7 '11 at 7:44
hero
611
closed as not a real question by Robert Harvey? Mar 7 '11 at 16:16
It's difficult to tell what is being asked here. This question is ambiguous, vag
ue, incomplete, overly broad, or rhetorical and cannot be reasonably answered in
its current form. For help clarifying this question so that it can be reopened,
visit the help center.
If this question can be reworded to fit the rules in the help center, please edi
t the question.
3
Aren't they already binary? zerkms Mar 7 '11 at 7:46
1
What is the format you call "binary mode"? Simon Mourier Mar 7 '11 at 7:46
1
You're not asking the question the smart way. Tell us what you're actually tryin
g to accomplish, and we'll be able to provide much better help. See here for mor
e question-asking tips: tinyurl.com/so-hints Cody Gray Mar 7 '11 at 7:47
1
If you can improve the question so that it is understandable, do so and flag for
moderator attention so we can reopen it. Robert Harvey? Mar 7 '11 at 16:16
add comment
2 Answers
activeoldestvotes
up vote
2
down vote
You can use BinaryReader to read your file. Please find code sample below:
public byte[] FileToByteArray(string _FileName)
{
byte[] buffer = null;
try
{
// Open file for reading
System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, Sys
tem.IO.FileMode.Open,System.IO.FileAccess.Read);
// attach filestream to binary reader
System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStr
eam);
// get total byte length of the file
long _TotalBytes = new System.IO.FileInfo(_FileName).Length;
// read entire file into buffer
_Buffer = _BinaryReader.ReadBytes((Int32)_TotalBytes);
// close file reader
_FileStream.Close();
_FileStream.Dispose();
_BinaryReader.Close();
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());

}
return _Buffer;
}
share|edit
answered Mar 7 '11 at 8:01
OrahSoft
389127
add comment
up vote
1
down vote
If what you want to do is read a binary file into a binary array object in c# th
en you can use a binary stream reader as described in this MSDN article.
share|edit
edited Jun 7 '12 at 3:02
Urda
1,94411228
answered Mar 7 '11 at 7:47
Sean Hunter
638412
add comment
Not the answer you're looking for? Browse other questions tagged c# or ask
your own question.

Вам также может понравиться