儲存文字檔
VB代码
需要引用 system 和 system.IO
方法1: 利用 StreamWriter 写入文档,值得注意的是,要确定要保存的文件夹已经创建,不然会报错。
1 2 3 | Dim sw As StreamWriter = New StreamWriter("C:\temp\test.txt") sw.Write("This is Test Text") sw.Close() |
方法2: 利用 My.Computer.FileSystem.WriteAllText 写入文档
1 | My.Computer.FileSystem.WriteAllText("C:\temp\test.txt", "This is test Text") |
方法3: 利用 System.IO.File.WriteAllText 写入文档
1 | System.IO.File.WriteAllText("c:\temp\test.txt", "this is test file") |
VB 代码
方法1:
1 2 3 4 5 6 | Dim sw As StreamWriter = New StreamWriter("C:\temp\test.txt") sw.Write("abc" & vbCrLf) sw.Close() Dim sw2 As StreamWriter = New StreamWriter("C:\temp\test.txt", True) sw2.Write("456" & vbCrLf) sw2.Close() |
方法2:
1 | My.Computer.FileSystem.WriteAllText("test.txt", "This is test Text", True) |
方法3:
1 | System.IO.File.AppendAllText("c:\temp\test.txt", "this is extra test file") |
VB 代码
1 2 3 4 5 | Dim sw As StreamWriter = New StreamWriter("C:\temp\test.txt") Dim strTemp As String = Me.RichTextBox1.Text strTemp = strTemp.Replace(vbLf, vbCrLf) sw.Write(strTemp) sw.Close() |
VB代码
1 | RichTextBox1.SaveFile("test.txt", RichTextBoxStreamType.PlainText) |