• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 930
  • Last Modified:

How to convert a string variable to System.IO.FileStream object in Vb.Net ?

I am working on VS 2012 Environment using Vb.Net Code.

I am reading a .csv file and loading the data to a string variable. Below syntax works fine.

[code]  Dim reader As StreamReader = New System.IO.StreamReader(File.OpenRead("D:\CSV\Test.csv"))
        Dim listA As New List(Of String)()
        Dim listB As New List(Of String)()
        Dim s As String = ""
        While Not reader.EndOfStream
            Dim line As String = reader.ReadLine()
            Dim values As String() = line.Split(";"c)
            s = s + line + Chr(10)
        End While

Open in new window

I have to Pass System.IO.FileStream object to this function.  How to convert string variable to FileStream object ?

  Private Function getEncodeType(ByVal theFile As System.IO.FileStream) As System.Text.Encoding
        Dim _enc As System.Text.Encoding = System.Text.Encoding.UTF8
        If (theFile.CanSeek) Then
            Dim bom(4) As Byte
            theFile.Read(bom, 0, 4)
            ' utf-8 
            ' ucs-2le, ucs-4le, and ucs-16le 
            ' utf-16 and ucs-2 
            If (bom(0) = &HEF AndAlso bom(1) = &HBB AndAlso bom(2) = &HBF) OrElse _
               (bom(0) = &HFF AndAlso bom(1) = &HFE) OrElse _
               (bom(0) = &HFE AndAlso bom(1) = &HFF) OrElse _
               (bom(0) = 0 AndAlso bom(1) = 0 AndAlso bom(2) = &HFE AndAlso bom(3) = &HFF) Then
                ' ucs-4 
                _enc = System.Text.Encoding.Unicode
                _enc = System.Text.Encoding.UTF8
            End If
        End If

        theFile.Seek(0, IO.SeekOrigin.Begin)

        Return _enc
    End Function

Open in new window

  • 2
2 Solutions
There is no conversion. You create a FileStream object and, when you do, you provide a String containing the file path. You're already doing that here:


Open in new window

If you want to use that FileStream again then, just as with any object or value, you assign it to a variable and then use that variable.
If you mean you want to write a string back to a file, you can use


chokkaAuthor Commented:
Okay !! Thank you !!, Looks like i have to create a Temp csv file.
chokkaAuthor Commented:
Thank you
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

7 new features that'll make your work life better

It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now