vb2008文件目录相关

前端之家收集整理的这篇文章主要介绍了vb2008文件目录相关前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

以下摘自《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:\Windows\Notepad.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,信息提示",MessageBoxButtons.OK)

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,MessageBoxButtons.OK)

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

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

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

MessageBox.Show(成功设置目录时间属性",MessageBoxButtons.OK)

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,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)

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)

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,MessageBoxButtons.OK)

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,MessageBoxButtons.OK)

End Sub

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

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

Dim MyPathName = "C:\Windows\Notepad.exe"

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

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

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

MessageBox.Show(MyInfo,MessageBoxButtons.OK)

End Sub

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

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

Dim MyPathName = "C:\Windows\Notepad.exe"

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

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

MessageBox.Show(MyInfo,MessageBoxButtons.OK)

End Sub

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

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

Dim MyPathName = "C:\Windows\Notepad.exe"

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

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

MessageBox.Show(MyInfo,MessageBoxButtons.OK)

End Sub

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

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

Dim MyPathName = "C:\Windows\Notepad.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,MessageBoxButtons.OK)

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,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,21)">".dat")

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

MessageBox.Show(MyInfo,MessageBoxButtons.OK)

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:\Windows\Notepad.exe",21)">"C:\Notepad.exe",True)

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

System.IO.File.Copy("C:\NotepadTest1.exe",True)

'方式二:使用方法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(True)

'方式三:使用方法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:\NotepadTest3.exe")

MessageBox.Show(使用三种方式更名文件操作成功!",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("E:\MyNotepad.exe",True)

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

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

MyInfo.CopyTo("E:\Notepad.exe",True)

MessageBox.Show(使用两种方式复制文件操作成功!",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:\MyNotepad.exe",True)

System.IO.File.Copy(True)

'方式一:使用方法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")) Then

MyInfo.Delete()

End If

MessageBox.Show(使用两种方式删除文件操作成功!",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:\Windows\NOTEPAD.exe"

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

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

MessageBox.Show(MyInfo,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,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,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 + 文件不存在!",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,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 = 编程技巧大全.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,MessageBoxIcon.Information)

Try

MyFileInfo.CreationTime = DateTime.Now

MyFileInfo.LastAccessTime = DateTime.Now

MyFileInfo.LastWriteTime = DateTime.Now

MessageBox.Show(成功设置文件时间属性",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 = 编程技巧大全.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 + 文件属性已经被成功设置!",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 = 编程技巧大全.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,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 = 编程技巧大全.doc"

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

MyFile.Attributes = IO.FileAttributes.Normal

MessageBox.Show(MyFileName + 文件属性已经被成功取消!",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,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 + 内容完全相同!",MessageBoxIcon.Information)

Else

MessageBox.Show(MyFileName1 + 内容不相同!",MessageBoxIcon.Information)

End If

Catch ex As Exception

MessageBox.Show(ex.Message,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(两个文件不相等。",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,1024)

While (MyBytesRead > 0)

MyBufferedOutput.Write(MyBuffer,MyBytesRead)

MyBytesRead = MyBufferedInput.Read(MyBuffer,1024)

End While

MyBufferedOutput.Flush()

MyBufferedInput.Close()

MyBufferedOutput.Close()

MessageBox.Show(复制文件操作完成!",MessageBoxIcon.Information)

Catch ex As Exception

MessageBox.Show(复制文件操作失败:" + ex.Message,MessageBoxIcon.Information)

End Try

End Sub

点击查看第二部分(文件存储)

猜你在找的VB相关文章