Equivalent of "Open fileName For Output As #1" VB6 to .NET

12,613
 Imports System
 Imports System.IO
 Imports System.Text
 Imports System.Collections.Generic

 Class Program

    Public Shared Sub Main(ByVal args As String())

    Dim mydocpath As String = _
    Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
    Dim sb As New StringBuilder()

    For Each txtName As String _
        In Directory.EnumerateFiles(mydocpath, "*.txt")
        Using sr As New StreamReader(txtName)
            sb.AppendLine(txtName.ToString())
            sb.AppendLine("= = = = = =")
            sb.Append(sr.ReadToEnd())
            sb.AppendLine()
            sb.AppendLine()

        End Using
    Next

    Using outfile As New StreamWriter(mydocpath & "\AllTxtFiles.txt", Encoding.Default)
        outfile.Write(sb.ToString())
    End Using
    End Sub
 End Class

http://msdn.microsoft.com/en-us/library/6ka1wd3w.aspx#Y0

Share:
12,613

Related videos on Youtube

Logan B. Lehman
Author by

Logan B. Lehman

Senior Software Architect @ JobzMall.

Updated on June 28, 2022

Comments

  • Logan B. Lehman
    Logan B. Lehman almost 2 years

    Another migration question,

    I have another chunk of VB6 code that seems to need some workaround for .NET. For a shortened version, this is all it is doing:

    Open sFileName For Output As #1
    Print #1,
    Print #1, "Facility:" & vbTab & Replace(Frame1.Caption, ",", " ")
    Print #1, 
    Print #1, "Address:" & vbTab & Replace(Me.lblAddr1.Caption, ",", " ")
    Print #1, "City/State:" & vbTab & Replace(Me.lblAddr2.Caption, ",", " ")
    

    And so on, and so forth. You can see it keeps repeating itself to create new lines. The question is, is how do I implement the same thing in .NET? Thanks for all the help guys.

    Logan

  • MarkJ
    MarkJ about 12 years
    You are writing the file with UTF-8 character encoding. VB6 would use "ANSI". It only matters if you need to write characters above ASCII 127. The cure is to specify Encoding.Default when you create the StreamWriter.
  • MarkJ
    MarkJ about 12 years
    Took the liberty of editing your answer to add Encoding.Default
  • Micah Armantrout
    Micah Armantrout about 12 years
    This is why I didn't edit my changes with the suggestion Default does not Guarantee to use ANSI Take a look ... stackoverflow.com/questions/838474/… blogs.msdn.com/b/shawnste/archive/2005/03/15/396312.aspx