Reputation: 999
Any help would be appreciated, I'm trying to convert the code below to C#, I've never used VB.NET so ReDim is a new one to me.
Thanks
Dim inFile As System.IO.FileStream
Dim binaryData() As Byte
Dim strFileName As String
strFileName = "C:\MyPicture.jpeg"
inFile = New System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)
''//Retrive Data into a byte array variable
ReDim binaryData(inFile.Length)
Dim bytesRead As Long = inFile.Read(binaryData, 0, CInt(inFile.Length))
inFile.Close()
Upvotes: 3
Views: 727
Reputation: 54087
If you're converting a lot of VB.NET to C#, you might want to check out VBConversions conversion tool.
Upvotes: 2
Reputation: 43207
Here's a permanant solution in case you need to do this again in future sometime.
Here are links for online VB to C# code converters and vice-versa. One is here and another is here.
LinkText1: http://www.developerfusion.com/tools/convert/vb-to-csharp/ LinkText2: http://converter.telerik.com/
Upvotes: 0
Reputation: 415600
ReDim re-allocates the array. Most of the time it's a code smell: a symptom or really wanting a collection type rather than an array. This code should do what you want:
string FileName = @"C:\MyPicture.jpeg";
byte[] binaryData;
long bytesRead;
using (var inFile = new System.IO.FileStream(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read) )
{
binaryData = new byte[inFile.Length];
bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
}
//I'm assuming you're actually doing something with each byte array here
Upvotes: 1
Reputation: 38346
This very easy to convert to C#.
FileStream inFile;
byte[] binaryData;
string strFileName;
strFileName = @"C:\MyPicture.jpeg";
inFile = new System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
binaryData = new byte[inFile.Length];
int bytesRead = inFile.Read(binaryData, 0, binaryData.Length);
inFile.Close();
But there is a much better way to write this.
string fileName = @"C:\MyPicture.jpeg";
byte[] binaryData = File.ReadAllBytes(fileName);
Upvotes: 3
Reputation: 1062512
Well, the closest translation would be:
binaryData = new byte[inFile.Length];
since it hasn't been assigned, or:
Array.Resize(ref binaryData,inFile.Length);
if it had been previously assigned. However, the code itself is very unsafe (you shouldn't assume Read
reads all the requested data); a much simpler approach here is:
binaryData = File.ReadAllBytes(strFileName);
Upvotes: 2
Reputation: 56934
I believe that the ReDim statement is just used to initialize the array:
byte[] binaryData;
binaryData = new byte[inFile.Lenght];
Upvotes: 2
Reputation: 4582
ReDim is used is resize arrays. (You can preserve the content if desired also. This code doesn't do that)
Upvotes: 0
Reputation: 136577
The code could be converted verbatim, but there's a much easier way to achieve what this is doing (read all bytes from a file), i.e.
var binaryData = File.ReadAllBytes(strFileName);
Personally I'd rename strFileName
to just fileName
as Hungarian notation is frowned upon in .NET code... but that's another matter!
Upvotes: 10