2012-03-12 76 views
4

另一個遷移問題,相當於「打開文件名輸出#1」 VB6到.NET

我的VB6代碼另一塊,這似乎需要一些解決方法.NET。對於縮短版本,這是它所做的全部:

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, ",", " ") 

依此類推,等等。你可以看到它不斷重複創建新的線條。問題是,我如何在.NET中實現同樣的東西?感謝所有幫助的人。

洛根

+0

只是StreamWriter的及其的WriteLine()方法。 – 2012-03-12 01:22:52

回答

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

+0

非常感謝! – 2012-03-12 01:56:29

+1

您正在使用UTF-8字符編碼寫入文件。 VB6將使用「ANSI」。只有當你需要在ASCII 127上面寫字符時才重要。治癒的方法是在創建StreamWriter時指定Encoding.Default。 – MarkJ 2012-03-12 07:35:39

+0

冒着編輯你的答案的自由添加'Encoding.Default' – MarkJ 2012-03-12 17:15:10