开发者

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

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For h开发者_JS百科elp clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

How can I convert any file like .exe or .doc .....etc to binary mode in c#


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, System.IO.FileMode.Open,System.IO.FileAccess.Read); 
      // attach filestream to binary reader 
      System.IO.BinaryReader _BinaryReader = new System.IO.BinaryReader(_FileStream); 
      // 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;
}


If what you want to do is read a binary file into a binary array object in c# then you can use a binary stream reader as described in this MSDN article.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜