搜索
您的当前位置:首页正文

vb对文件目录的操作

来源:独旅网
原文地址:vb2008 文件目录相关作者:esonbest

以下摘自《vb2008开发经验与实战宝典》 源码位置c01

'将指定URI数据下载到本地文件 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

Dim MyUri As String = \"http://www.microsoft.com/mspress/images/banner.gif\"

Dim MyFileName As String = \"banner.gif\"

Dim MyClient As New System.Net.WebClient()

MyClient.DownloadFile(MyUri, MyFileName)

System.Diagnostics.Process.Start(MyFileName)

End Sub

Public Class Form1

'判断指定目录是否已经存在

System.IO.Directory.Exists(MyDir1) '获取指定目录的上级目录

Dim MyParentDir = System.IO.Directory.GetParent(MyDir).FullName '获取全路径名的目录信息

Dim MyDirectoryName = System.IO.Path.GetDirectoryName(MyPathName)

'获取全路径名的根目录信息

Dim MyPathName = \"C:WindowsNotepad.exe\"

Dim MyRootDirectoryName = System.IO.Path.GetPathRoot(MyPathName)

'获取当前工作目录

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Handles Button5.Click

Dim MyPath = \"当前工作目录是:\"

MyPath += System.IO.Directory.GetCurrentDirectory()

MessageBox.Show(MyPath, \"信息提示\

End Sub

'设置当前工作目录

Dim MyPath = \"C:Windows\"

System.IO.Directory.SetCurrentDirectory(MyPath)

End Sub

'获取和设置指定目录的时间

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click

Dim MyDirName = \"F:Visual Basic 2005 编程技巧大全\"

Dim MyInfo = MyDirName + \"目录的时间信息如下:\"

MyInfo += vbCrLf + \"目录创建时间:\" + System.IO.Directory.GetCreationTime(MyDirName).ToString()

MyInfo += vbCrLf + \"目录访问时间:\" + System.IO.Directory.GetLastAccessTime(MyDirName).ToString()

MyInfo += vbCrLf + \"目录修改时间:\" + System.IO.Directory.GetLastWriteTime(MyDirName).ToString()

MessageBox.Show(MyInfo, \"信息提示\

System.IO.Directory.SetCreationTime(MyDirName, DateTime.Now)

System.IO.Directory.SetLastAccessTime(MyDirName, DateTime.Now)

System.IO.Directory.SetLastWriteTime(MyDirName, DateTime.Now)

MessageBox.Show(\"成功设置目录时间属性!\信息提示\

End Sub

'获取指定目录的属性

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

Dim MyDirName As String = \"C:TestDir\"

Dim MyInfo As String = MyDirName + \"目录的属性信息如下:\"

Try

Dim MyAttributes As System.IO.FileAttributes System.IO.File.GetAttributes(MyDirName)

If ((MyAttributes And System.IO.FileAttributes.ReadOnly) System.IO.FileAttributes.ReadOnly) Then

MyInfo += vbCrLf + \"只读属性为真;\"

End If

If ((MyAttributes And System.IO.FileAttributes.System) System.IO.FileAttributes.System) Then

MyInfo += vbCrLf + \"系统属性为真;\"

End If

If ((MyAttributes And System.IO.FileAttributes.Hidden) System.IO.FileAttributes.Hidden) Then

MyInfo += vbCrLf + \"隐藏属性为真;\"

End If

If ((MyAttributes And System.IO.FileAttributes.Archive) System.IO.FileAttributes.Archive) Then

MyInfo += vbCrLf + \"归档属性为真;\"

= = = = =

End If

MessageBox.Show(MyInfo, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

Catch ex As Exception

MessageBox.Show(ex.Message, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Try

End Sub

'设置指定目录的属性

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click

Dim MyDirName As String = \"C:TestDir\"

Dim MyAttributes As System.IO.FileAttributes

Try

System.IO.File.SetAttributes(MyDirName, System.IO.FileAttributes.Normal)

MyAttributes = System.IO.File.GetAttributes(MyDirName)

System.IO.File.SetAttributes(MyDirName, MyAttributes Or System.IO.FileAttributes.ReadOnly)

MyAttributes = System.IO.File.GetAttributes(MyDirName)

System.IO.File.SetAttributes(MyDirName, MyAttributes Or System.IO.FileAttributes.System)

MyAttributes = System.IO.File.GetAttributes(MyDirName)

System.IO.File.SetAttributes(MyDirName, MyAttributes Or System.IO.FileAttributes.Hidden)

MyAttributes = System.IO.File.GetAttributes(MyDirName)

System.IO.File.SetAttributes(MyDirName, MyAttributes Or System.IO.FileAttributes.Archive)

MyAttributes = System.IO.File.GetAttributes(MyDirName)

MessageBox.Show(\"成功设置目录属性!\信息提示\MessageBoxIcon.Information)

Catch ex As Exception

MessageBox.Show(ex.Message, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Try

End Sub

'取消指定目录的属性

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click

Dim MyDirName As String = \"C:TestDir\"

Try

System.IO.File.SetAttributes(MyDirName, System.IO.FileAttributes.Normal)

MessageBox.Show(\"成功取消目录属性!\信息提示\MessageBoxIcon.Information)

Catch ex As Exception

MessageBox.Show(ex.Message, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Try

End Sub

'获取启动程序的文件目录

Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

Handles Button11.Click

Dim MyInfo = \"启动了应用程序的可执行文件的目录是:\" + Application.StartupPath

MessageBox.Show(MyInfo, \"信息提示\

End Sub

'获取启动程序的文件路径

Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click

Dim MyInfo = \"启动了应用程序的可执行文件的路径是:\" + Application.ExecutablePath

MessageBox.Show(MyInfo, \"信息提示\

End Sub

'去掉全路径名的路径信息

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click

Dim MyPathName = \"C:WindowsNotepad.exe\"

Dim MyFileName = System.IO.Path.GetFileName(MyPathName)

Dim MyInfo = \"全路径文件名:\" + MyPathName + vbCrLf

MyInfo += \"无路径文件名:\" + MyFileName

MessageBox.Show(MyInfo, \"信息提示\

End Sub

'去掉全路径名的扩展名和路径

Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click

Dim MyPathName = \"C:WindowsNotepad.exe\"

Dim MyFileName = System.IO.Path.GetFileNameWithoutExtension(MyPathName)

Dim MyInfo = \"全路径文件名:\" + MyPathName + vbCrLf

MyInfo += \"无路径和扩展名的文件名:\" + MyFileName

MessageBox.Show(MyInfo, \"信息提示\

End Sub

'获取全路径名的扩展名信息

Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click

Dim MyPathName = \"C:WindowsNotepad.exe\"

Dim MyExtensionName = System.IO.Path.GetExtension(MyPathName)

Dim MyInfo = \"全路径文件名:\" + MyPathName + vbCrLf

MyInfo += \"扩展名信息:\" + MyExtensionName

MessageBox.Show(MyInfo, \"信息提示\

End Sub

'合并两个包含路径的字符串

Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click

Dim MyPathName = \"C:WindowsNotepad.exe\"

Dim MyNewPath = \"F:\"

Dim MyFileName = System.IO.Path.GetFileName(MyPathName)

Dim MyDestName = System.IO.Path.Combine(MyNewPath, MyFileName)

Dim MyInfo = \"源文件名:\" + MyPathName + vbCrLf

MyInfo += \"目标文件名:\" + MyDestName

MessageBox.Show(MyInfo, \"信息提示\

End Sub

'获取路径名禁止使用的字符

Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click

Dim MyChars() As Char = System.IO.Path.GetInvalidPathChars()

Dim MyInfo As String = \"路径名禁止使用字符包括:\" + vbCrLf

For Each MyChar As Char In MyChars

MyInfo += MyChar.ToString() + vbCrLf

Next

MessageBox.Show(MyInfo, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

'更改指定文件的扩展名

Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click

Dim MyOldFileName = \"C:atlog.txt\"

Dim MyResult = System.IO.Path.ChangeExtension(MyOldFileName, \".dat\")

Dim MyInfo = String.Format(\"成功更改文件扩展名:{0} 为: {1}\MyResult)

MessageBox.Show(MyInfo, \"信息提示\

End Sub

'以不同的方式更名文件

Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click

Try

'复制测试用文件

System.IO.File.Copy(\"C:WindowsNotepad.exe\

'方式一:使用方法File.Copy()

System.IO.File.Copy(\"C:Notepad.exe\

'方式二:使用方法FileInfo.MoveTo()

Dim MyInfo As New System.IO.FileInfo(\"C:Notepad.exe\")

If (System.IO.File.Exists(\"C:NotepadTest2.exe\")) Then

System.IO.File.Delete(\"C:NotepadTest2.exe\")

End If

MyInfo.MoveTo(\"C:NotepadTest2.exe\")

'复制测试用文件

System.IO.File.Copy(\"C:WindowsNotepad.exe\

'方式三:使用方法File.Move()

If (System.IO.File.Exists(\"C:NotepadTest3.exe\")) Then

System.IO.File.Delete(\"C:NotepadTest3.exe\")

End If

System.IO.File.Move(\"C:Notepad.exe\

MessageBox.Show(\"使用三种方式更名文件操作成功!\\"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

Catch ex As Exception

MessageBox.Show(ex.Message, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Try

End Sub

'以不同的方式复制文件

Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click

Try

'方式一:使用方法File.Copy()

System.IO.File.Copy(\"C:WindowsNotepad.exe\

'方式二:使用方法FileInfo.CopyTo()

Dim MyInfo As New System.IO.FileInfo(\"C:WindowsNotepad.exe\")

MyInfo.CopyTo(\"E:Notepad.exe\

MessageBox.Show(\"使用两种方式复制文件操作成功!\\"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

Catch ex As Exception

MessageBox.Show(ex.Message, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Try

End Sub

'以不同的方式删除文件

Private Sub Button21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button21.Click

Try

'复制测试用文件

System.IO.File.Copy(\"C:WindowsNotepad.exe\

System.IO.File.Copy(\"C:WindowsNotepad.exe\

'方式一:使用方法File.Delete()

If (System.IO.File.Exists(\"C:MyNotepad.exe\")) Then

System.IO.File.Delete(\"C:MyNotepad.exe\")

End If

'方式二:使用方法FileInfo.Delete()

Dim MyInfo As New System.IO.FileInfo(\"C:Notepad.exe\")

If (System.IO.File.Exists(\"C:Notepad.exe\")) Then

MyInfo.Delete()

End If

MessageBox.Show(\"使用两种方式删除文件操作成功!\\"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

Catch ex As Exception

MessageBox.Show(ex.Message, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Try

End Sub

'获取指定文件的尺寸大小

Private Sub Button22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button22.Click

Dim MyFileName As String = \"C:WindowsNOTEPAD.exe\"

Dim MyFileInfo As New System.IO.FileInfo(MyFileName)

Dim MyInfo As String = MyFileName + \"文件共有:\" + MyFileInfo.Length.ToString() + \"字节\"

MessageBox.Show(MyInfo, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

'计算多层目录的文件尺寸

Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.Click

Dim MyFolder As String = \"F:Visual Basic 2008 程序开发经验宝典\"

Dim MyDir As New System.IO.DirectoryInfo(MyFolder)

Dim MyInfo As String = MyFolder + \"目录的大小是:\" + CalculateDirectorySize(MyDir, True).ToString() + \"字节。\"

MessageBox.Show(MyInfo, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

Public Shared Function CalculateDirectorySize(ByVal MyDirectory As System.IO.DirectoryInfo, ByVal IsSubDirectories As Boolean) As Long

Dim MySize As Long = 0

'检查包含的所有文件

Dim MyFiles() As System.IO.FileInfo = MyDirectory.GetFiles()

For Each MyFile As System.IO.FileInfo In MyFiles

MySize += MyFile.Length

Next

'检查包含的所有子目录

If (IsSubDirectories) Then

Dim MyDirs() As System.IO.DirectoryInfo = MyDirectory.GetDirectories()

For Each MyDir As System.IO.DirectoryInfo In MyDirs

MySize += CalculateDirectorySize(MyDir, True)

Next

End If

Return MySize

End Function

'获取文件名禁止使用的字符

Private Sub Button24_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button24.Click

Dim MyChars() As Char = System.IO.Path.GetInvalidFileNameChars()

Dim MyInfo As String = \"文件名禁止使用字符包括:\" + vbCrLf

For Each MyChar As Char In MyChars

MyInfo += MyChar.ToString() + vbCrLf

Next

MessageBox.Show(MyInfo, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

'将长文件名转换成短文件名

Private Sub Button25_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button25.Click

Dim MyLongName As String = \"F:Visual C# 2008 编程技巧大全.doc\"

If (Not System.IO.File.Exists(MyLongName)) Then

MessageBox.Show(MyLongName + \"文件不存在!\\"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

Return

End If

Dim MyShortName As New System.Text.StringBuilder(256)

Dim MyInfo As String = vbCrLf + \"长文件名:\" + MyLongName + vbCrLf

GetShortPathName(MyLongName, MyShortName, 256)

MyInfo += \"短文件名:\" + MyShortName.ToString()

MessageBox.Show(MyInfo, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

Private Declare Function GetShortPathName Lib \"Kernel32.dll\" Alias \"GetShortPathNameA\" (ByVal lpszLongPath As String, ByVal lpszShortPath As System.Text.StringBuilder, ByVal cchBuffer As Integer) As Integer

'获取和设置指定文件的时间

Private Sub Button26_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button26.Click

Dim MyFileName As String = \"F:Visual C# 2008 编程技巧大全.doc\"

Dim MyFileInfo As New System.IO.FileInfo(MyFileName)

Dim MyInfo As String = MyFileName + \"文件的时间信息如下:\"

MyInfo += vbCrLf + \"文件创建时间:\" + MyFileInfo.CreationTime.ToString()

MyInfo += vbCrLf + \"文件访问时间:\" + MyFileInfo.LastAccessTime.ToString()

MyInfo += vbCrLf + \"文件修改时间:\" + MyFileInfo.LastWriteTime.ToString()

MessageBox.Show(MyInfo, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

Try

MyFileInfo.CreationTime = DateTime.Now

MyFileInfo.LastAccessTime = DateTime.Now

MyFileInfo.LastWriteTime = DateTime.Now

MessageBox.Show(\"成功设置文件时间属性!\\"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

Catch ex As Exception

MessageBox.Show(ex.Message, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Try

End Sub

'设置文件属性

Private Sub Button27_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button27.Click

Dim MyFileName As String = \"F:Visual C# 2008 编程技巧大全.doc\"

Dim MyFile As New System.IO.FileInfo(MyFileName)

'只读属性

MyFile.Attributes = MyFile.Attributes Or System.IO.FileAttributes.ReadOnly

'隐藏属性

MyFile.Attributes = MyFile.Attributes Or System.IO.FileAttributes.Hidden

'归档属性

MyFile.Attributes = MyFile.Attributes Or System.IO.FileAttributes.Archive

MessageBox.Show(MyFileName + \"文件属性已经被成功设置!\\"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

'获取文件属性

Private Sub Button28_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button28.Click

Dim MyFileName As String = \"F:Visual C# 2008 编程技巧大全.doc\"

Dim MyFile As New System.IO.FileInfo(MyFileName)

Dim MyInfo As String = MyFileName + \"文件属性信息如下:\" + vbCrLf

If ((MyFile.Attributes And System.IO.FileAttributes.ReadOnly) = System.IO.FileAttributes.ReadOnly) Then

MyInfo += \"只读属性:真\" + vbCrLf

End If

If ((MyFile.Attributes And System.IO.FileAttributes.Hidden) = System.IO.FileAttributes.Hidden) Then

MyInfo += \"隐藏属性:真\" + vbCrLf

End If

If ((MyFile.Attributes And System.IO.FileAttributes.Archive) = System.IO.FileAttributes.Archive) Then

MyInfo += \"归档属性:真\" + vbCrLf

End If

MessageBox.Show(MyInfo, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

'取消文件属性

Private Sub Button29_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button29.Click

Dim MyFileName As String = \"F:Visual C# 2008 编程技巧大全.doc\"

Dim MyFile As New System.IO.FileInfo(MyFileName)

MyFile.Attributes = IO.FileAttributes.Normal

MessageBox.Show(MyFileName + \"文件属性已经被成功取消!\\"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

'判断指定文件是否已经存在

Private Sub Button30_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button30.Click

Dim MyFile1 As String = \"C:atlog.txt\"

Dim MyFile2 As String = \"F:atlog.txt\"

Dim MyInfo As String = \"\"

If (System.IO.File.Exists(MyFile1)) Then

MyInfo += MyFile1 + \" 文件已经存在!\" + vbCrLf

Else

MyInfo += MyFile1 + \" 文件不存在!\" + vbCrLf

End If

If (System.IO.File.Exists(MyFile2)) Then

MyInfo += MyFile2 + \" 文件已经存在!\" + vbCrLf

Else

MyInfo += MyFile2 + \" 文件不存在!\" + vbCrLf

End If

MessageBox.Show(MyInfo, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Sub

'比较两个文件内容是否相同

Private Sub Button31_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button31.Click

Dim MyFileName1 As String = \"Compare1.txt\"

Dim MyFileName2 As String = \"Compare2.txt\"

Dim MyStream1 As System.IO.FileStream = Nothing

Dim MyStream2 As System.IO.FileStream = Nothing

Try

MyStream1 = New System.IO.FileStream(MyFileName1, System.IO.FileMode.Open)

MyStream2 = New System.IO.FileStream(MyFileName2, System.IO.FileMode.Open)

Dim MyCount1 As Integer = 0

Dim MyCount2 As Integer = 0

'逐一比较两个文件的每一个字节组,直到文件结束或有不相同的地方为止

While ((MyCount1 = MyCount2) And (MyCount1 <> -1))

'从文件中读取一个字节组

MyCount1 = MyStream1.ReadByte()

MyCount2 = MyStream2.ReadByte()

End While

If ((MyCount1 = MyCount2) And (MyCount1 <> 0)) Then

MessageBox.Show(MyFileName1 + \"和\" + MyFileName2 + \"的内容完全相同!\信息提示\

Else

MessageBox.Show(MyFileName1 + \"和\" + MyFileName2 + \"的内容不相同!\信息提示\

End If

Catch ex As Exception

MessageBox.Show(ex.Message, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

Finally

MyStream1.Close()

MyStream2.Close()

End Try

End Sub

'比较两个文件是否完全相等

Private Sub Button32_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button32.Click

Try

Dim MyFile1 As String = \"Compare1.txt\"

Dim MyFile2 As String = \"Compare2.txt\"

Dim MyHash As System.Security.Cryptography.HashAlgorithm = System.Security.Cryptography.HashAlgorithm.Create()

'计算第一个文件的哈希值

Dim MyStream1 As New System.IO.FileStream(MyFile1, System.IO.FileMode.Open)

Dim MyHashBytes1() As Byte = MyHash.ComputeHash(MyStream1)

MyStream1.Close()

'计算第二个文件的哈希值

Dim MyStream2 As New System.IO.FileStream(MyFile2, System.IO.FileMode.Open)

Dim MyHashBytes2() As Byte = MyHash.ComputeHash(MyStream2)

MyStream2.Close()

'比较两个哈希值

If (BitConverter.ToString(MyHashBytes1) = BitConverter.ToString(MyHashBytes2)) Then

MessageBox.Show(\"两个文件相等。\信息提示\MessageBoxIcon.Information)

Else

MessageBox.Show(\"两个文件不相等。\\"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End If

Catch ex As Exception

MessageBox.Show(ex.Message, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Try

End Sub

'使用缓冲流快速复制文件

Private Sub Button33_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button33.Click

Dim MySourceFile As String = \"F:Northwind.mdb\"

Dim MyTargetFile As String = \"C:NW.mdb\"

Dim MyInputStream, MyOutputStream As System.IO.Stream

Dim MyBufferedInput, MyBufferedOutput As System.IO.BufferedStream

Dim MyBuffer(1024) As Byte

Dim MyBytesRead As Integer

Try

'创建二进制流

MyInputStream = System.IO.File.OpenRead(MySourceFile)

MyOutputStream = System.IO.File.OpenWrite(MyTargetFile)

'创建二进制缓冲流

MyBufferedInput = New System.IO.BufferedStream(MyInputStream)

MyBufferedOutput = New System.IO.BufferedStream(MyOutputStream)

MyBytesRead = MyBufferedInput.Read(MyBuffer, 0, 1024)

While (MyBytesRead > 0)

MyBufferedOutput.Write(MyBuffer, 0, MyBytesRead)

MyBytesRead = MyBufferedInput.Read(MyBuffer, 0, 1024)

End While

MyBufferedOutput.Flush()

MyBufferedInput.Close()

MyBufferedOutput.Close()

MessageBox.Show(\"复制文件操作完成!\信息提示\MessageBoxIcon.Information)

Catch ex As Exception

MessageBox.Show(\"复制文件操作失败:\" + ex.Message, \"信息提示\MessageBoxButtons.OK, MessageBoxIcon.Information)

End Try

End Sub

点击查看第二部分(文件存储) 分享: 0 喜欢

阅读(83)┊ 评论 (0)┊ 收藏(0) ┊转载原文 ┊ 喜欢▼ ┊打印┊举报 已投稿到: 圈子

加载中,请稍候......

前一篇:[转载]荒岛挑战(1):招商银行(中)后一篇:[转载]汉王文本王OCR识别注意事项以下摘自《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, \"信息提示\MessageBoxIcon.Information)

Catch ex As Exception

MessageBox.Show(ex.Message, \"信息提示\MessageBoxIcon.Information)

End Try

End Sub

End Class

MessageBoxButtons.OK, MessageBoxButtons.OK,

因篇幅问题不能全部显示,请点此查看更多更全内容

Top