原文地址:vb2008 文件目录相关作者:esonbest以下摘自《vb2008开发经验与实战宝典》源码位置c01'将指定URI数据下载到本地文件Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.ClickDim MyUri As String = "/mspress/images/banner.gif"Dim MyFileName As String = "banner.gif"Dim MyClient As New .WebClient()MyClient.DownloadFile(MyUri, MyFileName)System.Diagnostics.Process.Start(MyFileName)End SubPublic 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.ClickDim 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.ClickDim 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.ClickDim MyDirName As String = "C:TestDir"Dim MyInfo As String = MyDirName + "目录的属性信息如下:"TryDim MyAttributes As System.IO.FileAttributes = System.IO.File.GetAttributes(MyDirName)If ((MyAttributes And System.IO.FileAttributes.ReadOnly) = System.IO.FileAttributes.ReadOnly) ThenMyInfo += vbCrLf + "只读属性为真;"End IfIf ((MyAttributes And System.IO.FileAttributes.System) = System.IO.FileAttributes.System) ThenMyInfo += vbCrLf + "系统属性为真;"End IfIf ((MyAttributes And System.IO.FileAttributes.Hidden) = System.IO.FileAttributes.Hidden) ThenMyInfo += vbCrLf + "隐藏属性为真;"End IfIf ((MyAttributes And System.IO.FileAttributes.Archive) = System.IO.FileAttributes.Archive) ThenMyInfo += vbCrLf + "归档属性为真;"End IfMessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, rmation)Catch ex As ExceptionMessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, rmation)End TryEnd Sub'设置指定目录的属性Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.ClickDim MyDirName As String = "C:TestDir"Dim MyAttributes As System.IO.FileAttributesTrySystem.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("成功设置目录属性!", "信息提示", MessageBoxButtons.OK, rmation)Catch ex As ExceptionMessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, rmation)End TryEnd Sub'取消指定目录的属性Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.ClickDim MyDirName As String = "C:TestDir"TrySystem.IO.File.SetAttributes(MyDirName, System.IO.FileAttributes.Normal)MessageBox.Show("成功取消目录属性!", "信息提示", MessageBoxButtons.OK, rmation)Catch ex As ExceptionMessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, rmation)End TryEnd Sub'获取启动程序的文件目录Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)Handles Button11.ClickDim MyInfo = "启动了应用程序的可执行文件的目录是:" + Application.StartupPathMessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK)End Sub'获取启动程序的文件路径Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.ClickDim MyInfo = "启动了应用程序的可执行文件的路径是:" + Application.ExecutablePathMessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK)End Sub'去掉全路径名的路径信息Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.ClickDim MyPathName = "C:WindowsNotepad.exe"Dim MyFileName = System.IO.Path.GetFileName(MyPathName)Dim MyInfo = "全路径文件名:" + MyPathName + vbCrLfMyInfo += "无路径文件名:" + MyFileNameMessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK)End Sub'去掉全路径名的扩展名和路径Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.ClickDim MyPathName = "C:WindowsNotepad.exe"Dim MyFileName = System.IO.Path.GetFileNameWithoutExtension(MyPathName)Dim MyInfo = "全路径文件名:" + MyPathName + vbCrLfMyInfo += "无路径和扩展名的文件名:" + MyFileNameMessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK)End Sub'获取全路径名的扩展名信息Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.ClickDim MyPathName = "C:WindowsNotepad.exe"Dim MyExtensionName = System.IO.Path.GetExtension(MyPathName)Dim MyInfo = "全路径文件名:" + MyPathName + vbCrLfMyInfo += "扩展名信息:" + MyExtensionNameMessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK)End Sub'合并两个包含路径的字符串Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.ClickDim MyPathName = "C:WindowsNotepad.exe"Dim MyNewPath = "F:"Dim MyFileName = System.IO.Path.GetFileName(MyPathName)Dim MyDestName = bine(MyNewPath, MyFileName)Dim MyInfo = "源文件名:" + MyPathName + vbCrLfMyInfo += "目标文件名:" + MyDestNameMessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK)End Sub'获取路径名禁止使用的字符Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.ClickDim MyChars() As Char = System.IO.Path.GetInvalidPathChars()Dim MyInfo As String = "路径名禁止使用字符包括:" + vbCrLfFor Each MyChar As Char In MyCharsMyInfo += MyChar.ToString() + vbCrLfNextMessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, rmation)End Sub'更改指定文件的扩展名Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.ClickDim MyOldFileName = "C:atlog.txt"Dim MyResult = System.IO.Path.ChangeExtension(MyOldFileName, ".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.ClickTry'复制测试用文件System.IO.File.Copy("C:WindowsNotepad.exe", "C:Notepad.exe", True)'方式一:使用方法File.Copy()System.IO.File.Copy("C:Notepad.exe", "C:NotepadTest1.exe", True)'方式二:使用方法FileInfo.MoveTo()Dim MyInfo As New System.IO.FileInfo("C:Notepad.exe")If (System.IO.File.Exists("C:NotepadTest2.exe")) ThenSystem.IO.File.Delete("C:NotepadTest2.exe")End IfMyInfo.MoveTo("C:NotepadTest2.exe")'复制测试用文件System.IO.File.Copy("C:WindowsNotepad.exe", "C:Notepad.exe", True)'方式三:使用方法File.Move()If (System.IO.File.Exists("C:NotepadTest3.exe")) ThenSystem.IO.File.Delete("C:NotepadTest3.exe")End IfSystem.IO.File.Move("C:Notepad.exe", "C:NotepadTest3.exe")MessageBox.Show("使用三种方式更名文件操作成功!", "信息提示", MessageBoxButtons.OK, rmation)Catch ex As ExceptionMessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, rmation)End TryEnd Sub'以不同的方式复制文件Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.ClickTry'方式一:使用方法File.Copy()System.IO.File.Copy("C:WindowsNotepad.exe", "E:MyNotepad.exe", True)'方式二:使用方法FileInfo.CopyTo()Dim MyInfo As New System.IO.FileInfo("C:WindowsNotepad.exe")MyInfo.CopyTo("E:Notepad.exe", True)MessageBox.Show("使用两种方式复制文件操作成功!", "信息提示", MessageBoxButtons.OK, rmation)Catch ex As ExceptionMessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, rmation)End TryEnd Sub'以不同的方式删除文件Private Sub Button21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button21.ClickTry'复制测试用文件System.IO.File.Copy("C:WindowsNotepad.exe", "C:MyNotepad.exe", True)System.IO.File.Copy("C:WindowsNotepad.exe", "C:Notepad.exe", True)'方式一:使用方法File.Delete()If (System.IO.File.Exists("C:MyNotepad.exe")) ThenSystem.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")) ThenMyInfo.Delete()End IfMessageBox.Show("使用两种方式删除文件操作成功!", "信息提示", MessageBoxButtons.OK, rmation)Catch ex As ExceptionMessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, rmation)End TryEnd Sub'获取指定文件的尺寸大小Private Sub Button22_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button22.ClickDim 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, rmation)End Sub'计算多层目录的文件尺寸Private Sub Button23_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button23.ClickDim MyFolder As String = "F:Visual Basic 2008 程序开发经验宝典"Dim MyDir As New System.IO.DirectoryInfo(MyFolder)Dim MyInfo As String = MyFolder + "目录的大小是:" + CalculateDirectorySize(MyDir, True).ToString() + "字节。