vb对文件目录的操作 下载本文

内容发布更新时间 : 2024/5/17 20:09:31星期一 下面是文章的全部内容请认真阅读。

原文地址: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 = \

Dim MyFileName As String = \

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 = \

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 = \

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 = \编程技巧大全\

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 = \

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 = \

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 = \

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)