File to byte[ ]
-
just change your max file size and filepath... const int MaxSize = 1000; string FilePath = @"C:\test.txt"; byte[] b = new byte[MaxSize]; FileStream f = new FileStream(FilePath,FileMode.Open); f.Read(b,0,(int)f.Length); Wender Oliveira .NET Programmer
-
just change your max file size and filepath... const int MaxSize = 1000; string FilePath = @"C:\test.txt"; byte[] b = new byte[MaxSize]; FileStream f = new FileStream(FilePath,FileMode.Open); f.Read(b,0,(int)f.Length); Wender Oliveira .NET Programmer
Wender Oliveira wrote: just change your max file size and filepath... const int MaxSize = 1000; string FilePath = @"C:\test.txt"; byte[] b = new byte[MaxSize]; FileStream f = new FileStream(FilePath,FileMode.Open); f.Read(b,0,(int)f.Length); No. I've seen it many times and it is just wrong. You end up with an array with then length 1000. How does that reflect the content of the array? It doesn't. The object processing the array has to analyze the data in order to find out the length of the actual user data (sometimes that might even be impossible). And what if MaxSize is much larger? Just read the entire file as originally asked for and if you want to introduce a limit then use something like this (and make sure the receiving object is aware of the fact that the data might be truncated):
byte[] MyArray = null;
const int MaxSize = 1000;
string FilePath = @"C:\test.txt";
FileInfo MyFile = new FileInfo(FilePath);if (MyFile.Length > MaxSize)
MyArray = new byte[MaxSize];
else
MyArray = new byte[MyFile.Length];Best regards Dennis