I need to convert a piece of code from VB to C#. what should I use in place of FileSystemObject and TextStream?
what the below code does is that it reads a file already present in a directory and adds the content of the file to the fields.
Private Sub Read_abc_File()
Dim FileSystem As FileSystemObject
Dim abcFile As TextStream
Dim abcLine As String, abcSection As String
Dim abcFilename As String
Const Read As Integer = 1
abcFilename = "abc.txt"
Set FileSystem = New FileSystemObject
If Not FileSystem.FileExists(abcFilename) Then
FileSystem = Null
Exit Sub
End If
Set abcFile = FileSystem.OpenTextFile(abcFilename, Read, False)
Do While abcFile.AtEndOfStream <> True
abcLine = abcFile.ReadLine
If abcLine > " " Then
If Left$(abcLine, 1) = "[" Then
abcSection = abcLine
Else
Select Case abcSection
Case "[Datafiles]"
DataFilename.AddItem abcLine
Case "[Locations]"
Location.AddItem abcLine
Case "[Formats]"
Format.AddItem abcLine
Case "[Categories]"
Category.AddItem abcLine
End Select
End If
End If
Loop
abcFile.Close
Set abcFile = Nothing
Set FileSystem = Nothing
End Sub
any suggestions/answers are appreciated.
Thanks!
Heres a code snippet to get you started, i think you should be able to complete the job.
using System; using System.IO; static void Main(string[] args) { string fileName = "abc.txt"; if (!File.Exists(fileName)) return; using (FileStream file = File.OpenRead(fileName)) using (StreamReader reader = new StreamReader(file)) { while (!reader.EndOfStream) { string line = reader.ReadLine(); } } }