C # Help convert this code from VB.NET to C #

Any help would be appreciated, I am trying to convert the code below to C #, I have never used VB.NET, so ReDim is new 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()
+3
source share
8 answers

The code can be converted verbatim, but there is a much simpler way to achieve what it does (read all the bytes from the file), i.e.

var binaryData = File.ReadAllBytes(strFileName);

Personally, I would strFileNameonly rename to fileName, since Hungarian notation in .NET text is ignored ... but that's another matter!

+10
source

It is 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);
+3
source

, ReDim :

byte[] binaryData;

binaryData = new byte[inFile.Lenght];
+2

, :

binaryData = new byte[inFile.Length];

, :

Array.Resize(ref binaryData,inFile.Length);

. ( , Read ); :

binaryData = File.ReadAllBytes(strFileName);
+2

VB.NET #, VBConversions.

+2

ReDim . : , . , :

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
+1

ReDim - resize. ( , , )

0

Here's a permanent solution if you need to do it again in the future.

Here are the links for online VB code converters to C # and vice versa. One of them is here , and the other is here .

LinkText1: http://www.developerfusion.com/tools/convert/vb-to-csharp/ LinkText2: http://converter.telerik.com/

0
source

Source: https://habr.com/ru/post/1706384/


All Articles