With...End With 允许对指定的对象执行一系列语句,而不必重新限定对象的名称。例如,若要修改单个对象的若干不同的属性,请将属性赋值语句放在 With...End With 中,这样只用引用一次对象,而不是在每个属性赋值中都引用它。
Directory.GetFileSystemEntries 方法 (String)返回指定目录中所有文件和子目录的名称。
Directory.GetFileSystemEntries (String) 返回指定目录中所有文件和子目录的名称。
Directory.GetFileSystemEntries (String, String) 返回与指定搜索条件匹配的文件系统项的数组。
从文件读取文本
方法一:
Using sr As StreamReader = New StreamReader("TestFile.txt")
Dim line As String
Do
line = sr.ReadLine()
Console.WriteLine(Line)
Loop Until line Is Nothing
sr.Close()
End Using
方法二:
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Public Class TextFromFile
Private Const FILE_NAME As String = "MyFile.txt"
Public Shared Sub Main()
If Not File.Exists(FILE_NAME) Then
Console.WriteLine("{0} does not exist.", FILE_NAME)
Return
End If
Using sr As StreamReader = File.OpenText(FILE_NAME)
Dim input As String
input = sr.ReadLine()
While Not input Is Nothing
Console.WriteLine(input)
input = sr.ReadLine()
End While
Console.WriteLine("The end of the stream has been reached.")
sr.Close()
End Using
End Sub
End Class
如何:向文件写入文本
方法一:
Using sw As StreamWriter = New StreamWriter("TestFile.txt")
sw.Write("This is the ")
sw.WriteLine("header for the file.")
sw.WriteLine(DateTime.Now)
sw.Close()
End Using
方法二:
Private Const FILE_NAME As String = "MyFile.txt"
Public Shared Sub Main()
If File.Exists(FILE_NAME) Then
Console.WriteLine("{0} already exists.", FILE_NAME)
Return
End If
Using sw As StreamWriter = File.CreateText(FILE_NAME)
sw.WriteLine("This is my file.")
sw.WriteLine("I can write ints {0} or floats {1}, and so on.", 1, 4.2)
sw.Close()
End Using
End Sub
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28311102/viewspace-749779/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/28311102/viewspace-749779/