2012-03-12 40 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