C# 如何将任何类型的文件转换为字节数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14770228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert a file of any type into byte array?
提问by Ramya
The file can be of any type ( png,bmp,jpeg,doc,docx,pdf) I have to convert the file into byte array and send to server via WebRequest where it will be converted to the corresponding file.
该文件可以是任何类型(png、bmp、jpeg、doc、docx、pdf)我必须将文件转换为字节数组并通过 WebRequest 发送到服务器,在那里它将被转换为相应的文件。
采纳答案by ybo
It's as easy as var bytes = File.ReadAllBytes(pathToFile)
就这么简单 var bytes = File.ReadAllBytes(pathToFile)
回答by bean
Use FileStream, you provide it the path to the file and can get the array from it.
使用 FileStream,您可以为其提供文件的路径,并可以从中获取数组。
回答by Yahia
You can use File.ReadAllBytesfor example... BEWARE that this loads the complete file into RAM...
例如,您可以使用File.ReadAllBytes... 请注意,这会将完整文件加载到 RAM 中...