Solved

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

Posted on 2014-09-22
4
810 Views
Last Modified: 2014-09-23
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)
            listA.Add(values(0))
            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
            Else
                _enc = System.Text.Encoding.UTF8
            End If
        End If

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

        Return _enc
    End Function

Open in new window

0
Comment
Question by:chokka
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Assisted Solution

by:VijayBalkawade
VijayBalkawade earned 250 total points
ID: 40338301
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:

File.OpenRead("D:\CSV\Test.csv")

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.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 40338697
If you mean you want to write a string back to a file, you can use

System.IO.File.WriteAllText

function.
0
 

Author Comment

by:chokka
ID: 40338913
Okay !! Thank you !!, Looks like i have to create a Temp csv file.
0
 

Author Closing Comment

by:chokka
ID: 40338914
Thank you
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
asp.net repeater server posting 5 38
GUID's in SQL Server 4 31
vb.net code 9 28
CSS styling problem 3 28
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decadeā€¦

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question