2010-06-26 460 views

回答

2

的一種方式是將數字寫入一個StringBuilder,然後用它的toString()方法得到的結果文本:

Imports System.IO 
Imports System.Text 


Public Class NumberWriter 
    Private ReadOnly OutputPath as String = _ 
      Path.Combine(Application.StartupPath, "out.txt") 


    Public Sub WriteOut() 
     Dim outbuffer as New StringBuilder() 

     For i as integer = 1 to 100 
      outbuffer.AppendLine(System.Convert.ToString(i)) 
     Next i 

     File.WriteAllText(OutputPath, outbuffer.ToString(), true) 
    End Sub 

    Public Shared Sub Main() 
     Dim writer as New NumberWriter() 
     Try 
     writer.WriteOut() 
     Catch ex as Exception 
     Console.WriteLine(ex.Message) 
     End Try 
    End Sub 
End Class 
1

有在家裏是一個很好的例子,在和學習,你可以嘗試

Dim FILE_NAME As String = "C:\test2.txt" 

If System.IO.File.Exists(FILE_NAME) = True Then 
    Dim objWriter As New System.IO.StreamWriter(FILE_NAME) 
    objWriter.Write(TextBox1.Text) 
    objWriter.Close() 
    MsgBox("Text written to file") 
Else 
    MsgBox("File Does Not Exist") 
End If 
1

您還可以使用 「My.Computer.FileSystem」 命名空間,如:

Dim str As String = "" 
For num As Int16 = 1 To 100 
    str += num.ToString & vbCrLf 
Next 
My.Computer.FileSystem.WriteAllText("C:\Working\Output.txt", str, False)