I want to convert following vb code to c#
Public Function ReadATextFile(fileName As String) As String
Dim FSO As New FileSystemObject
Dim Tsr As TextStream
Dim ReturnString As String
If FSO.FileExists(fileName) Then
Set Tsr = FSO.OpenTextFile(fileName, ForReading)
ReturnString = Tsr.ReadAll
Tsr.Close
ReadATextFile=ReturnString
End If
End Function
this routine just reads the file into string using FileSystemObject in VBA
I used the string str = File.ReadAllText(ofd.FileName); in c#
but problem here is when i explore the string from the VBA the length of the string is around 13000 and string from the c# the length is around 15000
why such difference? what extra characters are added through c#? Is it depend on encoding, compiler or any other factor…
The C# version will use UTF-8 encoding, which may explain the difference.
Try specifying an encoding explicitly, e.g.
Encoding.Defaultis the encoding for the operating system’s current ANSI page. Despite its name, it is not used as the default encoding for most methods that take an optional Encoding parameter.