4

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

Logan B. Lehman
  • 4,867
  • 7
  • 32
  • 45

1 Answers1

7
 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

MarkJ
  • 30,070
  • 5
  • 68
  • 111
Micah Armantrout
  • 6,781
  • 4
  • 40
  • 66
  • 1
    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 Mar 12 '12 at 07:35
  • Took the liberty of editing your answer to add `Encoding.Default` – MarkJ Mar 12 '12 at 17:15
  • This is why I didn't edit my changes with the suggestion Default does not Guarantee to use ANSI Take a look ... http://stackoverflow.com/questions/838474/why-encoding-ascii-asciiencoding-default-in-c http://blogs.msdn.com/b/shawnste/archive/2005/03/15/396312.aspx – Micah Armantrout Mar 12 '12 at 18:05