扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:潇潇 来源:YESKY 2007年10月14日
关键字: VB Windows API 小结
CreateDirectory, CreateDirectoryEx |
创建一个新目录 |
GetCurrentDirectory | 在一个缓冲区中装载当前目录 |
GetDiskFreeSpace,GetDiskFreeSpaceEx | 获取与一个磁盘的组织有关的信息,以及了解剩余空间的容量 |
GetDriveType | 判断一个磁盘驱动器的类型 |
GetFullPathName | 获取指定文件的完整路径名 |
GetLogicalDrives | 判断系统中存在哪些逻辑驱动器字母 |
GetLogicalDriveStrings | 获取一个字串,其中包含了当前所有逻辑驱动器的根驱动器路径 |
GetSystemDirectory | 这个函数能取得Windows系统目录(System目录)的完整路径名。在这个目录中,包含了所有必要的系统文件。根据微软的标准,其他定制控件和一些共享组件也可放到这个目录。通常应避免在这个目录里创建文件。在网络环境中,往往需要管理员权限才可对这个目录进行写操作 |
GetTempPath | 获取为临时文件指定的路径 |
GetVolumeInformation | 获取与一个磁盘卷有关的信息 |
GetWindowsDirectory | 这个函数能获取Windows目录的完整路径名。在这个目录里,保存了大多数windows应用程序文件及初始化文件 |
RemoveDirectory | 删除指定目录 |
SetCurrentDirectory | 设置当前目录 |
SetVolumeLabel | 设置一个磁盘的卷标(Label) |
‘定义常数变量 Private Const DRIVE_CDROM = 5 ‘表示光盘驱动器 Private Const DRIVE_FIXED = 3 ‘表示硬盘驱动器 Private Const DRIVE_RAMDISK = 6 ‘表示RAM驱动器 Private Const DRIVE_REMOTE = 4 ‘表示网络驱动器 Private Const DRIVE_REMOVABLE = 2 ‘表示软盘驱动器 ‘函数声明 Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long Private Sub Form_Load() Dim temp As Long temp = GetDriveType("e:\") ‘取的e:盘驱动器类型 Select Case temp ‘比较 Case DRIVE_CDROM MsgBox "DRIVE_CDROM: 光盘驱动器" Case DRIVE_FIXED MsgBox "DRIVE_FIXED: 硬盘驱动器" Case DRIVE_RAMDISK MsgBox "DRIVE_RAMDISK: RAM驱动器" Case DRIVE_REMOTE MsgBox "DRIVE_REMOTE: 网络驱动器" Case DRIVE_REMOVABLE MsgBox "DRIVE_REMOVABLE: 软盘驱动器" End Select End Sub |
Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" (ByVal lpRootPathName As String, lpFreeBytesAvailableToCaller As LARGE_INTEGER, lpTotalNumberOfBytes As LARGE_INTEGER, lpTotalNumberOfFreeBytes As LARGE_INTEGER) As Long |
参数 | 类型及说明 |
lpRootPathName | String,不包括卷名的磁盘根路径名 |
lpFreeBytesAvailableToCaller | LARGE_INTEGER,指定一个变量,用于容纳调用者可用的字节数量 |
lpTotalNumberOfBytes | LARGE_INTEGER,指定一个变量,用于容纳磁盘上的总字节数 |
lpTotalNumberOfFreeBytes | LARGE_INTEGER,指定一个变量,用于容纳磁盘上可用的字节数 |
Type LARGE_INTEGER ' 8 Bytes lowpart As Long highpart As Long End Type |
Private Type LARGE_INTEGER ' 8 Bytes,定义一个LARGE_INTEGER结构 lowpart As Long highpart As Long End Type ‘函数声明 Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" (ByVal lpRootPathName As String, lpFreeBytesAvailableToCaller As LARGE_INTEGER, lpTotalNumberOfBytes As LARGE_INTEGER, lpTotalNumberOfFreeBytes As LARGE_INTEGER) As Long ‘定义变量 Dim RootPathName As String Dim FreeBytesAvailabletoCaller As LARGE_INTEGER Dim TotalNumberOfBytes As LARGE_INTEGER Dim TotalNumberOfFreeBytes As LARGE_INTEGER Private Sub Form_Load() Dim temp As Long Dim tempa, tempb, tempc RootPathName = "c:" ‘取得磁盘空间 temp = GetDiskFreeSpaceEx(RootPathName, FreeBytesAvailabletoCaller, TotalNumberOfBytes, TotalNumberOfFreeBytes) ‘格式化文本,并显示在text控件 Text1.Text = Text1.Text + "磁盘容量:" + Chr(13) + Chr(10) tempa = TotalNumberOfBytes.highpart * 65635 + TotalNumberOfBytes.lowpart ‘计算容量 tempa = 0 – tempa ‘我的驱动器大于2G,要得到正确的值,进行了调整 Text1.Text = Text1.Text + CStr(tempa) + "字节" + Chr(13) + Chr(10) tempa = Format(tempa / 1024 / 1024 / 1024, "0.00") Text1.Text = Text1.Text + tempa + "G" + Chr(13) + Chr(10) ‘取得磁盘可用空间 Text1.Text = Text1.Text + "磁盘可用空间:" + Chr(13) + Chr(10) tempb = TotalNumberOfFreeBytes.highpart * 1000 + TotalNumberOfFreeBytes.lowpart ‘计算 Text1.Text = Text1.Text + CStr(tempb) + "字节" + Chr(13) + Chr(10) ’格式化文本并显示 tempb = Format(tempb / 1024 / 1024 / 1024, "0.00") Text1.Text = Text1.Text + tempb + "G" + Chr(13) + Chr(10) ‘取得磁盘已用空间 Text1.Text = Text1.Text + "磁盘已用空间:" + Chr(13) + Chr(10) tempa = 0 – tempa ‘调整 tempc = tempa – tempb ‘计算 tempc = 0 - tempc Text1.Text = Text1.Text + CStr(tempc) + "G" + Chr(13) + Chr(10)’格式化文本,并显示 End Sub |
Declare Function CreateDirectory& Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpNewDirectory As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) Declare Function CreateDirectoryEx& Lib "kernel32" Alias "CreateDirectoryExA" (ByVal lpTemplateDirectory As String, ByVal lpNewDirectory As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) |
参数 | 类型及说明 |
lpTemplateDirectory | String,指定一个模板目录的名字,从中复制默认属性(比如目录中文件的默认压缩方式)。如设为vbNullString,则表示不使用模板 |
lpNewDirectory | String,新目录的名字 |
lpSecurityAttributes | SECURITY_ATTRIBUTES,这个结构定义了目录的安全特性——如果操作系统支持的话 |
‘定义结构 Private Type SECURITY_ATTRIBUTES nLength As Long lpSecurityDescriptor As Long bInheritHandle As Long End Type ‘函数声明 Private Declare Function CreateDirectory& Lib "kernel32" Alias "CreateDirectoryA" (ByVal lpNewDirectory As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) Private Declare Function CreateDirectoryEx& Lib "kernel32" Alias "CreateDirectoryExA" (ByVal lpTemplateDirectory As String, ByVal lpNewDirectory As String, lpSecurityAttributes As SECURITY_ATTRIBUTES) Private Sub form_load() Dim temp As SECURITY_ATTRIBUTES CreateDirectory "c:\test", temp ‘创建文件夹 End Sub |
Declare Function RemoveDirectory Lib "kernel32" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long |
参数 | 类型及说明 |
lpPathName | 为String类型,要删除的那个目录的名字 |
‘函数声明 Private Declare Function RemoveDirectory Lib "kernel32" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long Private Sub form_load() Dim temp As String temp= “c:\test” RemoveDirectory temp ‘移除文件夹 End Sub |
Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long |
参数 | 类型及说明 |
LpPathName | String,新当前目录的路径 |
‘函数声明 Private Declare Function SetCurrentDirectory Lib "kernel32" Alias "SetCurrentDirectoryA" (ByVal lpPathName As String) As Long Private Sub form_load() SetCurrentDirectory "c:\" ‘设置当前目录 End Sub |
Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long |
参数 | 类型及说明 |
lpBuffer | String,用于装载系统目录路径名的一个字串缓冲区。它应事先初始化成nSize+1个字符的长度。通常至少要为这个缓冲区分配MAX_PATH个字符的长度 |
nSize | Long,lpBuffer字串的最大长度 |
‘函数声明 Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long Private Sub form_load() Dim buffer As String Dim size As Long size = 255 buffer = size + 1 GetSystemDirectory buffer, size ‘将系统路径存放在buffer中 MsgBox buffer ‘显示路径 End Sub |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者