2011-03-07 16 views
-2

如何将.exe或.doc等等任何文件转换为二进制模式在c#如何在c#中将任何文件转换为二进制模式?

+3

它们不是已经是二进制的吗? – zerkms 2011-03-07 07:46:07

+1

你称之为“二进制模式”的格式是什么? – 2011-03-07 07:46:55

+1

你不是在聪明地问这个问题。告诉我们你的实际*试图完成的事情,我们将能够提供更好的帮助。请参阅此处以获取更多问题提示:http://tinyurl.com/so-hints – 2011-03-07 07:47:25

回答

1

如果你想要做的是读取一个二进制文件到C#中的二进制数组对象,那么你可以如this MSDN文章中所述使用binary stream reader

1

您可以使用BinaryReader来读取您的文件。请在下面找到代码示例:

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; 
} 
相关问题