vb对文件目录的操作

vb对文件目录的操作
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 = "https://www.360docs.net/doc/178016168.html,/mspress/images/banner.gif"

Dim MyFileName As String = "banner.gif"

Dim MyClient As New https://www.360docs.net/doc/178016168.html,.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, "信息提示", 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, https://www.360docs.net/doc/178016168.html,rmation)

Catch ex As Exception

MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

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("成功设置目录属性!", "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

Catch ex As Exception

MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

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("成功取消目录属性!", "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

Catch ex As Exception

MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

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:WindowsNotepad.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:WindowsNotepad.exe"

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

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

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:WindowsNotepad.exe"

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

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

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:WindowsNotepad.exe"

Dim MyNewPath = "F:"

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

Dim MyDestName = https://www.360docs.net/doc/178016168.html,bine(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, "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

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}", 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: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")) Then

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

End If

MyInfo.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")) Then

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

End If

System.IO.File.Move("C:Notepad.exe", "C:NotepadTest3.exe")

MessageBox.Show("使用三种方式更名文件操作成功!", "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

Catch ex As Exception

MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

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", "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, https://www.360docs.net/doc/178016168.html,rmation)

Catch ex As Exception

MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

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", "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")) 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, https://www.360docs.net/doc/178016168.html,rmation)

Catch ex As Exception

MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

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, https://www.360docs.net/doc/178016168.html,rmation)

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, https://www.360docs.net/doc/178016168.html,rmation)

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, https://www.360docs.net/doc/178016168.html,rmation)

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, https://www.360docs.net/doc/178016168.html,rmation)

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, https://www.360docs.net/doc/178016168.html,rmation)

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 + "文件访问时间:" + https://www.360docs.net/doc/178016168.html,stAccessTime.ToString()

MyInfo += vbCrLf + "文件修改时间:" + https://www.360docs.net/doc/178016168.html,stWriteTime.ToString()

MessageBox.Show(MyInfo, "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

Try

MyFileInfo.CreationTime = DateTime.Now

https://www.360docs.net/doc/178016168.html,stAccessTime = DateTime.Now

https://www.360docs.net/doc/178016168.html,stWriteTime = DateTime.Now

MessageBox.Show("成功设置文件时间属性!", "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

Catch ex As Exception

MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

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, https://www.360docs.net/doc/178016168.html,rmation)

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, https://www.360docs.net/doc/178016168.html,rmation)

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, https://www.360docs.net/doc/178016168.html,rmation)

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, https://www.360docs.net/doc/178016168.html,rmation)

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 + "的内容完全相同!", "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

Else

MessageBox.Show(MyFileName1 + "和" + MyFileName2 + "的内容不相同!", "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

End If

Catch ex As Exception

MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

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 = https://www.360docs.net/doc/178016168.html,puteHash(MyStream1)

MyStream1.Close()

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

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

Dim MyHashBytes2() As Byte = https://www.360docs.net/doc/178016168.html,puteHash(MyStream2)

MyStream2.Close()

'比较两个哈希值

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

MessageBox.Show("两个文件相等。", "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

Else

MessageBox.Show("两个文件不相等。", "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

End If

Catch ex As Exception

MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, https://www.360docs.net/doc/178016168.html,rmation)

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

VBProject代码操作代码之常用语句

一、增加模块 1.增加一个模块,命名为“我的模块” ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_StdModule).Name = "我的模块" 系统常量vbext_ct_StdModule=1 2.增加一个类模块,命名为“我的类” ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_ClassModule).Name = "我的类" vbext_ct_ClassModule=2 3.增加一个窗体,命名为“我的窗体” ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_MSForm).Name = "我的窗体" vbext_ct_MSForm=3 二、删除模块 1.删除“模块1” ThisWorkbook.VBProject.VBComponents.Remove hisWorkbook.VBProject.VBComponents("模块1") 2.删除窗体“UserForm1” ThisWorkbook.VBProject.VBComponents.Remove ThisWorkbook.VBProject.VBComponents("UserForm1") 3.删除类模块“类1” ThisWorkbook.VBProject.VBComponents.Remove ThisWorkbook.VBProject.VBComponents("类1") 4.删除所有的窗体 Sub RmvForms() Dim vbCmp As VBComponent For Each vbCmp In ThisWorkbook.VBProject.VBComponents If vbCmp.Type = vbext_ct_MSForm Then ThisWorkbook.VBProject.VBComponents.Remove vbCmp Next vbCmp End Sub 相关: 工作表和ThisWorkbook的模块类型为vbext_ct_Document=100 三、增加代码 1.在“模块1”中插入代码 如果需要在“Sheet1”、“Thisworkbook”、或“Userform1”中操作,用只需将下面的“模块1”换成相应的名称即可。 方法1: 在模块的开始增加代码,增加的代码放在公共声明option,全局变量等后面。 Sub AddCode1() ThisWorkbook.VBProject.VBComponents("模块1").CodeModule.AddFromString _ "sub aTest()" & Chr(10) & _ "msgbox ""Hello""" & Chr(10) & _ "end sub" End Sub

VB常用字符串操作函数解读

VB常用字符串操作函数2009/11/25 18:321. ASC(X,Chr(X:转换字符字符码[格式]: P=Asc(X 返回字符串X的第一个字符的字符码 P=Chr(X 返回字符码等于X的字符 [范例]:(1P=Chr(65 ‘ 输出字符A,因为A的ASCII码等于65 (2P=Asc(“A” ‘ 输出65 2. Len(X:计算字符串X的长度 [格式]: P=Len(X [说明]:空字符串长度为0,空格符也算一个字符,一个中文字虽然占用2 Bytes,但也算 一个字符。 [范例]: (1 令X=”” (空字符串 Len(X 输出结果为0 (2 令X=”abcd” Len(X 输出结果为4 (3 令X=”VB教程” Len(X 输出结果为4 3. Mid(X函数:读取字符串X中间的字符 [格式]: P=Mid(X,n 由X的第n个字符读起,读取后面的所有字符。 P=Mid(X,n,m 由X的第n个字符读起,读取后面的m个字符。 [范例]: (1 X=”abcdefg” P=Mid(X,5 结果为:P=”efg” (2 X=”abcdefg” P=Mid(X,2,4 结果为 P=”bcde” 4. R eplace: 将字符串中的某些特定字符串替换为其他字符串 [格式]: P=Replace(X,S,R [说明]:将字符串X中的字符串S替换为字符串R,然后返回。[范例]:X=”VB is very good” P=Replace(X,good,nice 输出结果为:P=”VB is very nice” 5. StrReverse:反转字符串 [格式]: P=StrReverse(X [说明]:返回X参数反转后的字符串 [范例]:(1)X=”abc” P=StrReverse(X 输出结果:P=”cba” 6. Ucase(X,Lcase(X:转换英文字母的大小写 [格式]:P=Lcase(X ‘ 将X字符串中的大写字母转换成小写P=Ucase(X ‘ 将X字符串中的小写字母转换成大写 [说明]:除了英文字母外,其他字符或中文字都不会受到影响。 [范例]:(1)令X=”VB and VC” 则Lcase(X的结果为”vb and vc”,Ucase(X的结果为”VB AND VC” 7. InStr函数:寻找字符串 [格式]: P=InStr(X,Y 从X第一个字符起找出Y出现的位置 P=InStr(n,X,Y 从X第n个字符起找出Y出现的位置 [说明]:(1)若在X中找到Y,则返回值是Y第一个字符出现在X中的位置。(2) InStr(X,Y相当于 InStr(1,X,Y。(3)若字符串长度,或X为空字符串,或在X中找不到Y,则都 返回0。(4)若Y为空字符串,则返回0。 ---------------------------------------------------------------------------------------------- mid(字符串,从第几个开始,长度 ByRef 在[字符串]中[从第几个开始]取出[长度个字符串] 例如 mid("小欣无敌",1,3 则返回 "小欣无" instr(从第几个开始,字符串1,字符串2 ByVal 从规定的位置开始查找,返回字符

VB读写ini文件

VB读写ini文件(1) 2007-06-20 11:32 自从注册表诞生以来ini文件正在逐渐失去其市场占有率,然而基于ini文件的独立性,致使其还没有到达退出历史舞台的地步,很多应用程序的初始化和一些界面参数的设置仍然很愿意从ini文件中读取,为了保证操作需用参数对ini文件的读取的通明性,建议使用一个模块来完成此工作。注:所有操作调用标准的Win API函数来完成。 Dim Ret As Long Dim Start As Long Public FileName As String Const BufSize = 10240 Dim buf As String * BufSize Private Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long Public Sub SetValue(ByVal clsName As String, ByVal key As String, ByVal V As String) Ret = WritePrivateProfileString(clsName, key, V, FileName) End Sub Public Function GetValue(ByVal clsName As String, ByVal key As String) As String Ret = GetPrivateProfileString(clsName, key, "", buf, BufSize, FileName) Start = 1 GetValue = RetStr() End Function Private Function RetStr() As String Dim i As Long i = InStr(Start, buf, Chr(0)) If i > Start Then RetStr = Mid(buf, Start, i - Start) End If Start = i + 1 End Function 至此已经完成了对一个完整的独立模块的封装,接下来就来看看怎么引用(其实看完上面程序就明了了) VB读写INI文件(2) 2007-06-20 11:32 INI 文件是什么样子?——不会吧,这都不知道。INI 文件就是 Windows 中常见的以 .ini 为扩展名的文件,其内部格式和各部分的名称如下: [Section1] Key1=Value1

VB基本操作题

基本操作题,请根据以下各小题的要求设计Visual Basic应用程序(包括界面和代码) 1、(1)在名为Form1的窗体上绘制两个标签(名称分别为Label1和Label2,标题分别为“长”和“宽”)、两个文本框(名称分别为Text1和Text2,Text属性均为空白)和一个命令按钮(名称为Command1,标题为“输入”)。编写命令按钮的Click事件过程,使程序运行后,若单击命令按钮,则先后显示两个“输入”对话框,在两个“输入”对话框中分别输入长和宽,并分别在两个文本框中显示出来,运行后的窗体如图21-1所示。 注意:程序中不得使用任何变量;文件必须存放在考生文件夹中,工程文件名为sj1.vbp,窗体文件名为sj1.frm。 (2)在名为Form1的窗体上绘制一个标签(名称为Label1,标题为“输入”)、一个文本框(名称为Text1,Text属性为空白)和一个命令按钮(名称为Command1,标题为“显示”)。请编写命令按钮的Click事件过程,使程序运行后,在文本框中输入内容,然后单击命令按钮,则标签和文本框消失,并在窗体上显示文本框中的内容。运行后的窗体如图21-2和图21-3所示。 注意:要求程序中不得使用任何变量;文件必须存放在考生文件夹中,工程文件名为Sj2.vbp,窗体文件名为sj2.frm。 2、(1)在窗体上添加通用对话框控件并编写适当的程序代码,要求程序运行时,双击窗口,可以弹出“颜色”对话框。程序运行时的窗体界面如图62-1所示。 注意:保存时必须存放在考生文件夹下,窗体文件名为sj1.frm,工程文件名为sj2.vbp。 (2)在窗体上绘制出3个文本框,名称分别为Text1、Text2和Text3。要求程序运行时,焦点

VB获得Windows各类系统目录的方法

VB获得Windows各类系统目录的方法 现在有很多关于如何用VB获得Windows目录的文章,但大都只讲到如何获得Windows目录和System目录,有时候我们却需要获得像"我的文档"这样的目录("我的文档"的路径并不是固定的,可以由自己设定,也有可能因为系统的安装路径不同而不同),那又该如何处理呢?下面我们来具体谈谈如何用VB获得这种路径。 先向大家介绍两个API函数,这两个函数分别是SHGetSpecialFolderLocation和SHGetPathFromIDList,这就是我们用来获得各种路径的武器。 函数声明: Private Declare Function SHGetSpecialFolderLocation Lib "Shell32" (ByVal hwndOwner As Long, ByVal nFolder As Integer, ppidl As Long) As Long Private Declare Function SHGetPathFromIDList Lib "Shell32" Alias "SHGetPathFromIDListA" (ByVal pidl As Long, ByVal szPath As String) As Long 函数功能及参数说明: SHGetSpecialFolderLocation:获得某个特殊目录在特殊目录列表中的位置;它有三个参数,第一个参数是用来指定所有者窗口的,在应用中一般我们写上"0"就可以了;第二个参数是一个整数id,它决定要查找的目录是哪一个目录,它的取值可能如下: &H0& '桌面 &H2& '程序集 &H5& '我的文档 &H6& '收藏夹 &H7& '启动 &H8& '最近打开的文件 &H9& '发送 &HB& '开始菜单 &H13& '网上邻居 &H14& '字体 &H15& 'ShellNew &H1A& 'Application Data &H1B& 'PrintHood &H20& '网页临时文件 &H21& 'Cookies目录 &H22& '历史 第三个参数是获得的特殊目录在特殊目录列表中的地址。 SHGetPathFromIDList:根据某特殊目录在特殊目录列表中的地址获取该目录的准确路径。它有两个参数,第一个参数是特殊目录在特殊目录列表中的地址,也即上一个函数所获得的地址;第二个参数是一个字符串型数据,用来保存返回的特殊目录的准确路径。 比如:为了获得DeskTop的路径,首先需调用SHGetSpecialFolderLocation获得DeskTop在特殊目录列表中的位置Pid,然后调用SHGetPathFromIDList函数获得Pid指向的列表内容,即DeskTop的准确路径。 下面是我编写的一个用来获取Windows各种目录路径的例子,供大家参考。如果您有什么问题或建议,欢迎给我来信(xuhaoliang@https://www.360docs.net/doc/178016168.html,)。

VB 操作INI文件方法(经典详细教程)

1.定义一些变量 Public IniFileName As String, vbNullString As String, maxSize As Long, section1 As String, section2 As String 2.初始话这些变量 Public Function initial() IniFileName = App.Path & "" & "config.ini" vbNullString = "" maxSize = 255 section1 = "basics" section2 = "others" temp_str = String(255, 0) '建立缓冲区 End Function 3.声明INI函数 Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" ( _ ByVal lpApplicationName As String, _ ByVal lpKeyName As String, _ ByVal nDefault As Long, _ ByVal lpFileName As String) As Long Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( _ ByVal lpApplicationName As String, _ ByVal lpKeyName As Any, _ ByVal lpDefault As String, _ ByVal lpReturnedString As String, _ ByVal nSize As Long, _ ByVal lpFileName As String) As Long Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _ ByVal lpApplicationName As String, _ ByVal lpKeyName As Any, _ ByVal lpString As Any, _ ByVal lpFileName As String) As Long 4.调用函数 dim source as string

vb读取txt文件到textbox

vb读取txt文件到textbox vb读取txt文件到textbox 1.怎么通过代码创建一个文本文件,并读取,更新内容 以上为随即方式打开的文本文件 dim gfilenum as integer gfilenum = FreeFile Open "文件路径及文件名" For Random As gfilenum len=3 \'以随即方式打开一文件如果文件不存在就新建 用get #gfilenum ,记录在文件中位置,要放取得的数据的变量\'读取操作 用put #gfilenum ,记录在文件中位置,要放着要写入数据的变量\'写操作 close #filenum\'关闭文件 以下为顺序方式打开的文件 dim gfilenum as integer gfilenum = FreeFile Open "文件路径及文件名" For output As gfilenum \'以写入方式打开文本 print #gfilenum,要写入的文本 write #filenumber,要写入的文本 dim gfilenum as integer gfilenum = FreeFile Open "文件路径及文件名" For input As gfilenum \'以读出方式打开文本 input #gfilenum ,用来放读取的内容的内存变量名 还可用line input#,input()等读取更详细的查msdn 2.VB读取文本文件时,调用TextStream 对象中使用OpenTextFile报错了。 如果是 Set f = fs.OpenTextFile("E:\\table\\trace.txt", forreading, True, TristateUseDefault)

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 = "https://www.360docs.net/doc/178016168.html,/mspress/images/banner.gif" Dim MyFileName As String = "banner.gif" Dim MyClient As New https://www.360docs.net/doc/178016168.html,.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)

VB_NET语言操作txt文件代码

做二次开发过程中的一些代码搜集和个人的试错及注释: Dim str_File_1 As String = https://www.360docs.net/doc/178016168.html,puter.FileSystem.ReadAllText("E:\Visual Studio\Visual Basic\GUI\WindowsApplication2\Resources\1.tcl", System.Text.Encoding.ASCII) '根据实际的编码读第一个文件 Dim str_File_2 As String = https://www.360docs.net/doc/178016168.html,puter.FileSystem.ReadAllText("E:\Visual Studio\Visual Basic\GUI\WindowsApplication2\Resources\2.tcl", System.Text.Encoding.ASCII) '根据实际的编码读第二个文件 FileOpen(1, "E:\Visual Studio\Visual Basic\GUI\WindowsApplication2\Resources\1.tcl", OpenMode.Input, OpenAccess.Default, OpenShare.Shared) '以读方式打开文件 FileOpen(2, "E:\Visual Studio\Visual Basic\GUI\WindowsApplication2\Resources\2.tcl", OpenMode.Output, OpenAccess.Default, OpenShare.Shared) '以写方式打开文件 Print(2, str_File_1) '将1文件写入2中 FileClose(1) FileClose(2) Imports System.IO Public Class TXTClass '为了能够操作txt文本文档,需要在类的前面加入“System.IO”空间的引用; '然后定义3个变量sw(用于write操作,变量类型StreamWriter)、 'sr(用于read操作,变量类型StreamReader)、 'strRead()(用于保存从文本文档中读入的数据,变量类型String数组); '然后写一个空的sub New()过程(类必须有的)。 Private swAsStreamWriter Private srAsStreamReader Public strRead() As String Public Sub New() End Sub '写操作 '写入一行数据到文本文件 '以追加的形式写入一行数据 Public Sub ZhuiJiaSingle(ByValParth As String, ByValstrline As String) sw = New StreamWriter(Parth, True, System.Text.Encoding.Default) sw.WriteLine(strline) sw.Flush() sw.Close() sw = Nothing

(环境管理)实验VB编程环境和文件

实验1 环境和文件 实验目的: 1.熟悉VB的集成开发环境 2.掌握常用控件的属性、方法 3.熟悉常用对象事件的使用 4.熟悉文件系统控件的使用 5.掌握文件的打开、关闭和读写操作 实验1.1认识Visual Basic 实验任务: 认识Visual Basic的集成开发环境,熟悉各个窗口的功能,熟悉控件的属性、方法,熟悉事件的使用。实验结果界面如图1-1所示。 图 1-1 实验1.1运行界面 实验步骤: 1.启动VB6.0,创建一个“标准EXE”类型的应用程序。 2.将窗体的Font属性设为宋体、小二、粗体,Caption属性设置为“我的第一个程序”。 3.在窗体上添加两个命令按钮Command1(Caption属性设置为“欢迎”)和Command2(Caption属性设置为“再见”) 。 4.双击“欢迎”按钮,涉及如下代码: Print “欢迎使用Visual Basic” 5.双击“再见”按钮,添加如下代码: End 6.将窗体和工程分别以文件名vb1.frm和vb1.vbp保存在自己的文件夹中。 7.单击F5,试验运行本程序,直至满意为止。

说明:这里,“自己的文件夹”指任意磁盘获U盘如D盘根目录下以自己的名字和学号命名的子目录(例如姓名为“赵阳”,学号为09080126;则自己的文件夹就是“D:\赵阳09080126”),以后所有的实验若无特殊说明均保存在该目录下。 思考:除了上述方法,还可以怎样实现上述题目要求?试一试。 实验1.2 对象移动动画 实验任务: 熟悉Move方法实现对象移动,进一步熟悉控件事件的使用。实验结果界面如图1-2所示。 图1-2 实验1.2运行界面 实验步骤: 1.将窗体的Caption属性设为“欢迎新同学”;为窗体设置Picture属性(图片可以从C:\Windows\Web\Wallpaper中取,也可放自己喜欢的照片),设置窗体不可改变大小。 2.在窗体上添加两个标签,Caption属性为“欢迎新同学”,并将其设置为浮雕效果(提示:浮雕效果可以由两个背景风格为透明、前景颜色不同、位置稍有错位

VB6.0基础入门教程

VB最简单入门教程

目录 (提示:按ctrl,并单击鼠标,可以跳到相应页) 1.1 1 我们需要什么 (3) 1.2 2 第二章:事件、属性及数据类型 (3) 1.3 3 第三章:VB语言 (6) 1.4 4-1第四章按钮(一) (12) 1.5 4- 第四章按钮(二) (199) 1.6 5 第五章菜单 (27) 1.7 6-1第六章输入(一) (34) 1.8 6-2第六章输入(二) (42) 1.9 6-3第六章输入(三) (52) 1.10 7第七章输出 (59) 附录 (63) 2.1 VB教程 (63)

1.1 1 我们需要什么 我们需要什么?当我们在一个精彩的游戏世界中游历了一番之后,或是惊叹于某一工具软件的小巧精致之余,多少总会产生些许编程的冲动。编程吗,在以前如果你对电脑还是一个门外汉,那实在是一件可望而不可及的事情。如果运用基于DOS下的编程语言,譬如C、Qbasic、Pascal等等,真不知何年何月才能有所成就。 编程是需要天赋的,你必须在大脑中对整个程序有一个清晰的轮廓,一个高效的流程,这并不是每个人都能做到的,你必须思之慎之,这也使编程变成一件最枯燥无味的事情。但在第四代计算机语言(可视化编程)出现后,可以确切的说它开发了人们的更多天赋,并不局限于那些头脑异常清晰的人,每一个人都可以发现自己也可以编出一些从前不敢问津的程序,想象力的充分发挥才是第四代语言的精粹。 Visual Basic(以下简称VB)可以说是可视化语言的先驱了,而且它也是可视化程度最高的一个,从几年前VB诞生之日起到现在,它已经经历了五个版本,而且现在微软正在紧张的进行着VB6.0的研制、测试,这么高的更新率,不外乎说明两个问题:用户对VB的热衷,微软对VB的重视。不可否认微软对市场的预测能力是极为高明的,而它强大的技术、财力支持也使它在许多以前未进入的领域,在不长的时间内有成为最有力的竞争对手,如IE之于浏览器领域,《帝国时代》之于游戏都是最好的例证。对于VB现在也有一个很强的竞争对手――Delphi,有人把它称作VB杀手,这显然有偏激之处,VB的确有它的不足之处,但Delphi 又何尝不是呢,而且以微软对VB的倾心,VB的功能必然会越来越强大。 VB的诞生 VB的出现可以说是Microsoft Windows的日渐成熟的必然产物。Microsoft Windows为程序员和最终用户提供了一个共同的人机界面。对用户,Windows提供了一个图形鼠标的操作环境,该环境对所有的应用程序都一样;对于程序员,Windows提供了一组预定义工具----称之为Microsoft Windows 的软件开发工具箱(SDK),该工具能使程序员建立一个与Windows界面相同的应用程序,而且,程序员不必关心最终用户的硬件配置情况。在这一开发环境中,程序员唯一困难的是Microsoft SDK提供了六百多个函数和与其一致的事件驱动(event-driven)编程技术。两种新方法的交叉使众多的程序员重新陷入困境,程序员不仅要掌握程序驱动编程技术和六百多个函数的功能,而且还得用C语言描述这些问题。因此一般情况下,程序员首先要掌握C程序设计技术,而后再开始学习SDK。这样的条件下就要求在Microsoft多任务环境下出现一种操作方便,使用简单的新工具----Visual Basic由此诞生。 何为可视 英文Visual的意思是“视觉的”,“可视的Baisc”这个名字可能抽象了点,但实际上它却是最直观的编程方法,之所以叫做“可视”,你只要看到VB的界面就会明白,实际上你无需编程,就可以完成许多步骤。在VB中引入了控件的概念,在Windows中控件的身影无处不在,各种各样的按钮、文本框、无线钮,都是控件的种类,VB把这些控件模式化,并且每个控件都有若干属性用来控制控件的外观,工作方法。这样你就可以象在画板上一样,随意点几下鼠标,一个按钮就完成了,这些在以前的编程语言下是要经过相当复杂的工作的。

(完整版)vb连接access数据库及数据读写操作

ACCESS数据库和VB的连接 Edited by Ryan 2013 1、建立Access数据库 2、启动VB,建立标准EXE 图1 3、添加ActiveX控件 鼠标指向任意VB控件,单击右键,选择“部件(O)”,出现图3所示界面 图2

图3 选择部件“Microsoft ADO Data Control 6.0 (OLEDB)”,出现如图4所示控件 图4

4、添加控件Adodc 图5 4、在控件Adodc上添加数据源 鼠标指向控件Adodc1,单击右键,选择“ADODC 属性”,弹出如图7所示界面 图6

图7 单击“生成(U). . .”,弹出如图8所示界面 图8 选择“Microsoft Jet 4.0 OLE DB Provider”,单击“下一步(N) >>”,弹出如图9所示界面

图9 单击“. . .”,添加数据源(第一步所建Access 数据库),如图10所示 图10 单击“测试连接(T)”,出现提示框,如图11所示 图11 之后点击“确定”,退回到如图12所示界面

图12 单击“记录源”,弹出如图13所示界面 图13 在“命令类型”下,选择“1 - adCmdText”,在“命令文本(SQL)”下,输入“Select * from test1”,最后单击“应用”,“确定”即可 之后,进入程序书写部分 程序部分需要注意接头形式及简单例子如下: Private Sub Command1_Click() ‘VB按钮控件 Dim mydb As New ADODB.Connection ‘定义新的数据库连接 mydb.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\read database\test1\test1.mdb" ‘数据库绝对路径 Dim rs As New ADODB.Recordset ‘定义数据库的一个对象 mydb.Open ‘打开数据库 rs.Open "select * from test1", mydb, 3, 3 ‘打开数据库中的表test1

VB_Open_文件操作类函数功能详解

VB Open 文件操作类函数功能详解: 1、Open 文件名[For方式] [Access存取类型] [锁定] AS [#]文件号[Len=记录长度] 功能: 为文件的输入输出分配缓冲区,并确定缓冲区所使用的存取方式 说明: (1)打开方式: 指定文件的输入输出方式,可选,默认是Random。可以是以下值: a、Output:指定顺序输出方式,将覆盖原有内容。 b、Input:指定顺序输入方式。 c、Append:指定顺序输出方式,在文件未尾追加内容。 d、Random:指定随机存取方式,也是默认方式,在Random方式时,如果没有Access子句,则在执行Open语句时,VB将按下列顺序打开文件:读/写、只读、只写。 e、Binary:指定二进制文件。在这种方式下,可以用Get和Put语句对文件中任何字节位置的信息进行读写。在Binary方式中,如果没有Access子句,则打开文件的类型与Random方式相同。 (2)存取类型: 放在关键字Access之后,用来指定访问文件的类型。可以是下列类型之一: a、Read:打开只读文件。 b、Write:打开只写文件。 c、Read Write:打开读写文件。这种类型只对随机文件、二进制文件及用Appe nd方式打开的文件有效。 (3)锁定类型: 该子句只在多用户或多进和环境中使用,用来限制其他用户或其他进程对打开进行读写操作。锁定类型包括: a、默认:如不指定锁定类型,则本进程可以多次打开文件进行读写;在文件打开期间,其他进程不能对该文件执行读写操作。 b、Lock Shared:任何机器上的任何进程都可以对该文件进行读写操作。 c、Lock Read:不允许其他进程读该文件。只在没有其他Read存取类型的进程访问该文件时,才允许这种锁定。 d、Lock Write:不允许其他进程写这个文件。只在没有其他Write存取类型的进程访问该文件时,才允许这种锁定。 e、Lock Read Write:不允许其他进程读写这个文件。 如果不使用lock子句,则默认为Lock Read write。 (4)文件号: 由用户自行指定一个由1~511之间的整数,只要该文件号未被使用就合法;打开文件后,可以用该文件号进行读写等操作。

vb读取文本文件内容并打印输出

读取文本文件内容并打印输出 Private Sub Command1_Click() Dim TextLine As String Open "c:\\testfile.txt" For Input As #1 Do While Not EOF(1) Line Input #1, TextLine Print TextLine Loop Close #1 End Sub 新建文件并写入文件内容 Private Sub Form_Load() Const ForReading = 1, ForWriting = 2 Dim fso, f Dim SkipLineInFile As String Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("c:\\testfile.txt", ForWriting, True) f.Write "Hello world!" & vbCrLf & "VB Script is fun!" Set f = fso.OpenTextFile("c:\\testfile.txt", ForReading) SkipLineInFile = f.readall Debug.Print SkipLineInFile End Sub 冒泡上浮 Private Sub Command1_Click() Dim d(1 To 4) As Integer d(1) = Val(Text1.Text) d(2) = Val(Text2.Text) d(3) = Val(Text3.Text) d(4) = Val(Text4.Text) For i = 1 To 3 For j = 4 To i + 1 Step -1 If d(j) < d(j - 1) Then t = d(j) d(j) = d(j - 1) d(j - 1) = t End If Next j Next i For i = 1 To 4

vb6的文件操作

总结一下VB6的文件操作,省得要用的时候又到处查找。 一、文件类型 1、顺序文件(文本文件):以ASCII码形式存放的文件。似乎还有Unicode码存放的,有没有BCD码的呢? 2、随机访问文件:这种文件格式很有特点:文件中存放若干条等长的单元(也可以说是记录);每个单元包含同类型、等数量、等长度的数据项;文件中,除了字符串以ASCII码存放之外,其它都以二进制形式直接存放,节省存储空间。这种文件类型非常适合存储需要和软件交互的数据,如结构体数据、类数据等。这也是VB特有的文件访问方式。 3、二进制文件:以二进制形式存放,PE程序文件一般都是这个类型。 二、操作方法 1、顺序文件 打开:Open 文件名For Input | Output | Append As [#]文件号 Input打开读入,文件不存在报错。Output打开覆盖写入,文件不存在则创建。Append打开追加写入,文件不存在则创建。 读:Line Input #文件号, 字符串变量

读一行数据存入字符串变量,数据包括空格、Tab、等,不包括回车符和换行符,所以要显示文件的换行效果,要手动添加回车符和换行符(vbCrLf) Input #文件号, 变量1[, | ;] [变量2]... 这种方式可以存入多个变量,而且变量类型不限于字符串型。写:Print #文件号, 参数1[, | ;] [参数2]... 将各参数逐个写入文件。参数间用逗号隔开时,文件中相应插入多个空格;用分号隔开时,插入一个空格。可以用Spc(n)、Tab(n)等进行排版。 Write #文件号, 参数1[, | ;] [参数2]... 这种写入方式将自动添加界定符,对不同参数的类型加以界定。 2、随机访问文件 打开:Open 文件名[For Random] As [#]文件号Len = 记录长度 For Random可以省略,文件不存在则创建,读写都是这种打开方式。Len是文件中记录的一条长度,用以识别记录的开始与结束,经常用Len(记录名)来获取长度。 读:Get [#]文件号, [记录号], 变量

VB基本语法

1.1 VB的数据类型 数据类型是数据的表示和存储形式。VB定义的基本数据类型如表5-1所示。 表5-1 VB的基本数据类型 Integer(整型)、Long(长整型)、Single(单精度浮点型)、Double(双精度长整型)及Currency(货币型)均为Numeric数据类型。其中整型是16位的整数,长整型是32位的整数。单精度及双精度浮点型就表示实数,常被用于数量较大的数字。Currency数据类型支持小数点右面15位,是定点数据类型,适用于货币计算。 String(字符串变量)的数据类型只存放文本,是一个字符序列,它的每一个字符用ASCII 编码表示。不包含任何字符的串称为空串。 Byte数据类型表示0到255之间的数,常用于访问二进制文件、图形和声音文件

等。当需要把数据存放成字节,必须访问各字节时可采用Byte类型。 取值仅为True/False的类型称为Boolean(布尔)型,缺省值为False。 Date数据类型用于以特殊方式存放日期和时间。 Object变量可引用应用程序中或某些其他应用程序中的对象。 VB缺省规定,如果在生命中没有说明数据类型,则变量的数据类型为Variant(变体)。Variant数据类型还包含三种特定值:Empty、Null、Error。Empty值用于确定是否已将一个值赋予所创建的变量。赋值之前,Variant变量具有值Empty。 Null常用于数据库应用程序,表示未知或丢失为空的数据。 Error值是指已发出的过程中的错误状态。 Variant数据类型是最为灵活的数据类型,可以依据不同的需要进行各种数据类型的转换。它实际上包含两部分信息,一部分是数据类型的信息,一部分是表示数据值的信息。但正是由于它的灵活性,又使得它存在着一个明显的缺点,Variant数据类型比其他类型的数据占有更多的内存空间。 2 数组 VB中数组的定义类似于变量定义,所不同的是数组需要指定数组中的元素个数,例如:Dim Array(9)As Integer 数组中包含10个元素,脚标从0到9。 也可以指定脚标的起始值,例如: Dim IntegerArray(2 to 10)As Integer 这个数组含有九个元素,脚标从2到10。 还可以定义多维数组: Dim ThreeD(4,2 to 5,3 to 6)As Integer

(完整版)vb连接access数据库及数据读写操作.docx

ACCESS数据库和 VB 的连接 Edited by Ryan 2013 1、建立 Access 数据库 2、启动 VB,建立标准EXE 图 1 3、添加 ActiveX 控件 鼠标指向任意VB 控件,单击右键,选择“部件(O)”,出现图 3 所示界面图2

图3 选择部件“ Microsoft ADO Data Control 6.0 (OLEDB) ”,出现如图 4 所示控件图4

4、添加控件Adodc 图5 4、在控件 Adodc 上添加数据源 鼠标指向控件Adodc1 ,单击右键,选择“ADODC 属性”,弹出如图7 所示界面图6

图7 单击“生成(U). . .”,弹出如图8 所示界面 图8 选择“ Microsoft Jet 4.0 OLE DB Provider”,单击“下一步(N) >>”,弹出如图 9 所示界面

图9 单击“ . . .”,添加数据源(第一步所建Access 数据库),如图 10 所示 图10 单击“测试连接(T)”,出现提示框,如图11 所示 图11 之后点击“确定” ,退回到如图12 所示界面

图12 单击“记录源” ,弹出如图13 所示界面 图13 在“命令类型”下,选择“ 1 - adCmdText”,在“命令文本( SQL)”下,输入“ Select * from test1 ”,最后单击“应用”,“确定”即可 之后,进入程序书写部分 程序部分需要注意接头形式及简单例子如下: Private Sub Command1_Click()‘VB按钮控件 Dim mydb As New ADODB.Connection ‘定义新的数据库连接 mydb.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\read database\test1\test1.mdb" ‘数据库绝对路径 Dim rs As New ADODB.Recordset ‘定义数据库的一个对象 mydb.Open‘打开数据库 rs.Open "select * from test1", mydb, 3, 3‘打开数据库中的表test1

相关文档
最新文档