以下摘自《vb2008开发经验与实战宝典》
'读取和写入独立存储文件
Private Sub Button34_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles
Button34.Click
Dim MyStore As
System.IO.IsolatedStorage.IsolatedStorageFile = _
System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForAssembly()
'在独立存储中创建一个文件夹
MyStore.CreateDirectory("MyFolder")
'创建独立存储文件
Dim MyStream As System.IO.Stream = New System.IO. _
IsolatedStorage.IsolatedStorageFileStream("MyFile1.txt", _
System.IO.FileMode.Create, MyStore)
'Dim MyStream As System.IO.Stream = New
System.IO. _
'
IsolatedStorage.IsolatedStorageFileStream("MyFile2.txt",
_
'
System.IO.FileMode.Create, MyStore)
'Dim MyStream As System.IO.Stream = New
System.IO. _
'
IsolatedStorage.IsolatedStorageFileStream("MyFile3.txt",
_
'
System.IO.FileMode.Create, MyStore)
'Dim MyStream As System.IO.Stream = New
System.IO. _
'
IsolatedStorage.IsolatedStorageFileStream("MyFile4.txt",
_
'
System.IO.FileMode.Create, MyStore)
'Dim MyStream As System.IO.Stream = New
System.IO. _
'
IsolatedStorage.IsolatedStorageFileStream("MyFile5.txt",
_
'
System.IO.FileMode.Create, MyStore)
Dim MyWriter As New
System.IO.StreamWriter(MyStream, _
System.Text.Encoding.UTF8)
MyWriter.WriteLine("C:\文件夹中的文件包括:")
For Each MyFileName As String
In
System.IO.Directory.GetFiles("C:")
MyWriter.WriteLine(MyFileName)
Next
MyWriter.Flush()
MyWriter.Close()
MyStream.Close()
Dim MyInfo As String =
""
'独立存储区中的信息
MyInfo += "当前尺寸:
" + MyStore.CurrentSize.ToString() + vbCrLf
MyInfo += "存储区范围:
" + MyStore.Scope.ToString() + vbCrLf
MyInfo += "包含的文件有:"
+
vbCrLf
Dim MyFiles() As String =
MyStore.GetFileNames("*.*")
For Each MyFile As String
In MyFiles
MyInfo += MyFile + vbCrLf
Next
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End Sub
'读取和写入一个文本文件
Private Sub Button35_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles
Button35.Click
'创建(写入)一个文本文件
Dim MyStream As New
System.IO.FileStream("MyText.txt",
System.IO.FileMode.Create)
Dim MyWriter As New
System.IO.StreamWriter(MyStream,
System.Text.Encoding.UTF8)
MyWriter.WriteLine("C:\文件夹中的文件包括:")
For Each MyFileName As String
In
System.IO.Directory.GetFiles("C:")
MyWriter.WriteLine(MyFileName)
Next
MyWriter.Flush()
MyWriter.Close()
MyStream.Close()
'读取一个文本文件
Dim MyReader As New
System.IO.StreamReader("MyText.txt",
System.Text.Encoding.UTF8)
Dim
MyInfo As String = MyReader.ReadToEnd()
MyReader.Close()
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End Sub
'读取和写入一个二进制文件
Private Sub Button36_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles
Button36.Click
Try
'创建(写入)一个二进制文件
Dim MyStream As New
System.IO.FileStream("MyBinary.dat",
System.IO.FileMode.Create)
Dim MyWriter As New
System.IO.BinaryWriter(MyStream)
For
i As Integer = 0 To 30 Step
+1
MyWriter.Write(i)
Next
MyWriter.Close()
MyStream.Close()
'读取一个二进制文件
MyStream = New
System.IO.FileStream("MyBinary.dat",
System.IO.FileMode.Open, System.IO.FileAccess.Read)
Dim MyReader As New
System.IO.BinaryReader(MyStream)
Dim MyInfo As String =
""
For i As Integer
= 0 To 30 Step +1
MyInfo += MyReader.ReadInt32().ToString() + vbCrLf
Next
MyReader.Close()
MyStream.Close()
MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK,
MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK,
MessageBoxIcon.Information)
End Try
End Sub
End Class