.NET System.IO
d The file system classes are separated into two types of classes: informational and utility.
d The informational classes include: FileInfo, DirectoryInfo, DriveInfo. These classes expose all the system information about file system objects - files, directories, and drives.
d The utility classes include: File, Directory, and Path. These classes provide static/shared methods to perform certain operations on file system objects - files, directories, and paths.
d The Path class deals only with the string of a path. It makes no changes to the file sytem. For instance:
Dim OnePath As String = "C:\boot.int"
Path.ChangeExtention(OnePath, "bak")
^ 以上代码改变了一个Path,但是并没有改变文件名本身,就是说,OnePath改变成"C:\boot.bak",但是boot.ini本身的文件名并没有被改变。
How to read from a file:
1. One way of reading a file is using StreamReader classes, which makes reading easier than calling the Read or ReadByte methods of the Stream class.
Dim OneFile As FileStream = File.Open("C:\boot.ini", FileMode.Open, FileAccess.Read)
Dim OneReader As StreamReader = New StreamReader(OneFile)
Console.Write(OneReader.ReadToEnd())
OneReader.Close()
OneFile.Close()
@ The StreamReader class is intented to read a stream as a string, not as a series of bytes. Thus, the StreamReader's methods for returning data all return either strings or arrays of strings.
2.The File class has a simpler way to open a file for reading. It supports creating a StreamReader directly with the OpenText method:
Dim OneReader As StreamReader = File.OpenText("C:\boot.int")
Console.Write(OneReader.ReadToEnd())
OneReader.Close()
3. If all you need to do is read out the entire file, the File class supports reading the file in a single method call, hiding all the details of the stream and reader implementation by calling its ReadAllText method:
Console.Write(File.ReadAllText("C:\boot.ini"))
How to create a file for writing:
1. We can use the StreamWriter to write text directly into a new file
Dim OneFile As FileStream = File.Create("C:\SomeFile.txt")
Dim OneWriter As StreamWriter = New StreamWriter(OneFile)
OneWriter.WriteLine("Hello")
OneWriter.Close()
OneFile.Close()
2. File class supports creating a StreamWriter object directly with the CreateText method:
Dim OneWriter As StreamWriter = File.CreateText("C:\SomeFile.txt")
OneWriter.WriteLine("Hello")
OneWriter.Close()
3. File class aslo supports the WriteAllText method that write a string to a new file:
File.WriteAllText("C:\SomeFile.txt", "Hello")
How to open an existing file for writing:
1. File class
Dim OneFile As FileStream = File.Open("C:\SomeFile.txt", FileMode.Open, FileAccess.Write)
2. The File class has the OpenWrite method, which is a shortcut for opening existing files for writing:
Dim OneFile = File.OpenWrite("C:\SomeFile.txt")
How to exither open an existing file or create a new file for writing:
1. Using the Open method of the File class to specify that you want open or create. The FileMode.OpenOrCreate enumeration value allows you to avoid writing procedural code to deal with the issue of whether you are dealing with a new or existing file:
Dim OneFile As FileStream = FileOpen("C:\SomeFile.txt", FileMode.OpenOrCreate, FileAccess.Write)