【编者按】网学网ASP.net频道为大家收集整理了“VB.NET实现向文件写入文本“提供大家参考,希望对大家有所帮助!
一个VB.NET实现向文件写入文本的代码。
Imports System
Imports System.IO
Class Test
Public Shared Sub Main()
'' Create an instance of StreamWriter to write text to a file.
Dim sw As StreamWriter = New StreamWriter("TestFile.txt")
'' Add some text to the file.
sw.Write("This is the ")
sw.WriteLine("header for the file.")
sw.WriteLine("-------------------")
'' Arbitrary objects can also be written to the file.
sw.Write("The date is: ")
sw.WriteLine(DateTime.Now)
sw.Close()
End Sub
End Class
[C#]
using System;
using System.IO;
class Test
{
public static void Main()
{
// Create an instance of StreamWriter to write text to a file.
// The using statement also closes the StreamWriter.
using (StreamWriter sw = new StreamWriter("TestFile.txt"))
{
// Add some text to the file.
sw.Write("This is the ");
sw.WriteLine("header for the file.");
sw.WriteLine("-------------------");
// Arbitrary objects can also be written to the file.
sw.Write("The date is: ");
sw.WriteLine(DateTime.Now);
}
}
}
下面的代码示例创建一个新文本文件并向其写入一个字符串。
[Visual Basic]
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Public Class TextToFile
Private Const FILE_NAME As String = "MyFile.txt"
Public Shared Sub Main()
If File.Exists(FILE_NAME) Then
本新闻共2页,当前在第1页 1 2