一、磁盘分区的基本概念

1.磁盘分区(Patitions):

分区就是物理存储设备分割成多个不同的逻辑上的存储设备。分区从实质上说就是对硬盘的一种格式化。当我们创建分区时,就已经设置好了硬盘的各项物理参数,指定了硬盘主引导记录(即Master Boot

Record,一般简称为MBR)和引导记录备份的存放位置。

2.卷(Volumes)

卷,也称逻辑驱动器,是NTFS、FAT32等文件系统组织结构的最高层。

3.Windows系统中文件命名的标准

文件命名各位为“主文件名+扩展名”,中间以“.”隔开。文件可以没有扩展名,但不能没有主文件名。

用户给文件命名必须要遵守的规则

(1)长度规则。

windows系统不对单个文件的长度做限制,但文件路径的程度被限制在260个字符。因此文件名的长度为260减去文件所在目录的路径长度。

(2)特殊字符

特殊字符不能在文件名的任何位置出现。\ | / * ? < > " :

特殊字符在系统或命令行下代表一下特殊的意义。

二、文件系统的主要API

1. 磁盘和驱动器管理类API

GetLogicalDrives 获取主机中所有逻辑驱动器,以 Bit Map 的形式返回

GetLogicalDrivesStrings 获取主机中所有逻辑驱动器,以驱动器根路径字符串返回

FindFirstVolume 查找主机中的第一个驱动器,返回查找句柄

FindNextVolume 根据FindFirstVolume返回的句柄,查找主机中后续的逻辑驱动器

FindVolumeClose 关闭驱动器查找句柄

GetDriveType 获取驱动器类型

GetVolumeInformation 获取逻辑驱动器信息

FindFirstVolumeMountPoint 查找指定卷的第一个挂载点,返回查找句柄

FindNextVolumeMountPoint 根据FindFirstVolumeMountPoint返回的句柄,查找卷的后续挂载点

FindVolumeMountPointClose 关闭挂载点查找句柄

GetVolumeNameForVolumeMountPoint 根据指定挂载点获取相应的卷设备名

SetVolumeMountPoint 将指定卷挂载到指定挂载点

GetDiskFreeSpace 获取磁盘空间信息,包括每簇的扇区数,每扇区的字节数,簇数量,空闲的簇数量

GetDiskFreeSpaceEx 获取用户可用的空闲空间的字节数,磁盘总容量的字节数

Windows API 提供给用户态使用。对一些磁盘的高级操作,比如磁盘分区,格式化驱动器,改变驱动器的文件系统,读取磁盘扇区等操作需要在系统内核中完成,用户态API不能完成这些高级操作

2.文件和目录管理

DeleteFile 删除参数所指定文件

CopyFile 复制指定文件为一个新文件

MoveFile 将指定文件或目录移动到指定位置

CreateFile 新建或打开一个文件,获取文件句柄

ReadFile 读取由文件句柄指定的文件的内容

WriteFile 向由文件句柄指定的文件中写入内容

GetFileSize 获取文件大小,返回DWORD,大小超过DWORD最大值时可指定高32位DWORD联合存储

GetFileSizeEx 获取文件大小,存储到一个64位的大整数联合体中

CreateDirectory 创建一个目录

GetCurrentDirectory 获取当前程序所在目录

SetCurrentDirectory 设置当前程序所在目录

GetModuleFileName 获取当前模块全路径

FindFirstFile 查找指定目录下第一个文件或目录,获得查找句柄

FindNextFile 根据FindFirstFile获得的句柄,循环查找文件和目录

GetFileAttributes 获取指定文件或目录的属性,返回DWORD

GetFileAttributesEx 获取指定文件或目录属性,存储在WIN32_FILE_ATTRIBUTE_DATA结构体中

SetFileAttributes 将文件属性设置为指定值

FileTimeToLocalFileTime 将文件时间转换为本地时间

FileTimeToSystemTime 将文件时间转换为系统时间,SYSTEMTIME格式,便于显示

3.高级文件系统操作

CreateFileMapping 创建文件映射对象

MapViewOfFile 创建视图,将创建的文件映射对象映射到当前进程的地址空间中

FlushViewOfFile 将视图中数据写入磁盘,对视图的操作都会反映到磁盘上的文件中

OpenFileMapping 打开已经存在的命名的文件映射对象

UnmapViewOfFile 取消文件映射

GetMappedFileName 从映射对象获取被映射文件的文件设备名

QueryDosDevice 获取MS-DOS设备名

3.举例子

3.1 使用GetLogicalDriveStrings 获取磁盘驱动器根路径

int main()
{
CHAR szLogicalDriveStrings[BUFSIZE];
ZeroMemory(szLogicalDriveStrings, BUFSIZE);//这也是一个API函数, 清空这个字符串
GetLogicalDriveStrings(BUFSIZE - 1, szLogicalDriveStrings); printf("%s\n", szLogicalDriveStrings);
//C : \\ \0 D : \\ \0
PCHAR psz; psz = (PCHAR)szLogicalDriveStrings;
do{
printf("%s\n", psz);
psz += (strlen(psz) + 1);
} while ((*psz) != '\0'); system("pause");
}

1) szLogicalDriveStrings 在内存中的内容为: C : \ \0 D : \ \0

2)用一个指针指向szLogicalDriveStrings,循环使用strleen最终打印出 驱动器的根路径

3.2 遍历驱动器,并获得驱动器属性


/* 头文件 */
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
/* 预定义 */
#define BUFSIZE 1024
/* 函数申明 */
BOOL GetDirverInfo(LPSTR szDrive); /* ************************************
* 功能 应用程序主函数,遍历驱动器并调用
* GetDirverInfo 获取驱动器属性
**************************************/
void main(void)
{
CHAR szLogicalDriveStrings[BUFSIZE];
PCHAR szDrive; ZeroMemory(szLogicalDriveStrings, BUFSIZE);
// 获取逻辑驱动器卷标名
GetLogicalDriveStrings(BUFSIZE - 1, szLogicalDriveStrings);
szDrive = (PCHAR)szLogicalDriveStrings;
// 循环处理每个卷
do
{
if (!GetDirverInfo(szDrive))
{
printf("\nGet Volume Information Error: %d", GetLastError());
}
szDrive += (lstrlen(szDrive) + 1);
} while (*szDrive != '\x00'); system("pause");
} /* ************************************
* BOOL GetDirverInfo(LPSTR szDrive)
* 功能 获取驱动器的属性
* 参数 LPSTR szDrive
* 指明要获取属性的驱动器的根路径 如 C:\
* 返回值 BOOL 是否成功
**************************************/
BOOL GetDirverInfo(LPSTR szDrive)
{
UINT uDriveType;
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD dwFileSystemFlags;
TCHAR szFileSystemNameBuffer[BUFSIZE];
printf("\n%s\n", szDrive);
uDriveType = GetDriveType(szDrive);
// 判断类型
switch (uDriveType)
{
case DRIVE_UNKNOWN:
printf("The drive type cannot be determined. ");
break;
case DRIVE_NO_ROOT_DIR:
printf("The root path is invalid, for example, no volume is mounted at the path. ");
break;
case DRIVE_REMOVABLE:
printf("The drive is a type that has removable media, for example, a floppy drive or removable hard disk. ");
break;
case DRIVE_FIXED:
printf("The drive is a type that cannot be removed, for example, a fixed hard drive. ");
break;
case DRIVE_REMOTE:
printf("The drive is a remote (network) drive. ");
break;
case DRIVE_CDROM:
printf("The drive is a CD-ROM drive. ");
break;
case DRIVE_RAMDISK:
printf("The drive is a RAM disk. ");
break;
default:
break;
}
if (!GetVolumeInformation(
szDrive, NULL, 0,
&dwVolumeSerialNumber,
&dwMaximumComponentLength,
&dwFileSystemFlags,
szFileSystemNameBuffer,
BUFSIZE
))
{
return FALSE;
}
printf("\nVolume Serial Number is %u", dwVolumeSerialNumber);
printf("\nMaximum Component Length is %u", dwMaximumComponentLength);
printf("\nSystem Type is %s\n", szFileSystemNameBuffer); if (dwFileSystemFlags & FILE_SUPPORTS_REPARSE_POINTS)
{
printf("The file system does not support volume mount points.\n");
}
if (dwFileSystemFlags & FILE_VOLUME_QUOTAS)
{
printf("The file system supports disk quotas.\n");
}
if (dwFileSystemFlags & FILE_CASE_SENSITIVE_SEARCH)
{
printf("The file system supports case-sensitive file names.\n");
}
//you can use these value to get more informaion
//
//FILE_CASE_PRESERVED_NAMES
//FILE_CASE_SENSITIVE_SEARCH
//FILE_FILE_COMPRESSION
//FILE_NAMED_STREAMS
//FILE_PERSISTENT_ACLS
//FILE_READ_ONLY_VOLUME
//FILE_SUPPORTS_ENCRYPTION
//FILE_SUPPORTS_OBJECT_IDS
//FILE_SUPPORTS_REPARSE_POINTS
//FILE_SUPPORTS_SPARSE_FILES
//FILE_UNICODE_ON_DISK
//FILE_VOLUME_IS_COMPRESSED
//FILE_VOLUME_QUOTAS
printf("...\n"); return TRUE;
}
C:\
The drive is a type that cannot be removed, for example, a fixed hard drive.
Volume Serial Number is 3963567720
Maximum Component Length is 255
System Type is NTFS
The file system does not support volume mount points.
The file system supports disk quotas.
The file system supports case-sensitive file names.
... D:\
The drive is a type that cannot be removed, for example, a fixed hard drive.
Volume Serial Number is 1558048823
Maximum Component Length is 255
System Type is NTFS
The file system does not support volume mount points.
The file system supports disk quotas.
The file system supports case-sensitive file names.
... E:\
The drive is a type that cannot be removed, for example, a fixed hard drive.
Volume Serial Number is 4205893873
Maximum Component Length is 255
System Type is NTFS
The file system does not support volume mount points.
The file system supports disk quotas.
The file system supports case-sensitive file names.
... F:\
The drive is a type that cannot be removed, for example, a fixed hard drive.
Volume Serial Number is 2159020960
Maximum Component Length is 255
System Type is NTFS
The file system does not support volume mount points.
The file system supports disk quotas.
The file system supports case-sensitive file names.

3.3 使用FindFirstVolume系列函数遍历驱动器,获取驱动器信息

#include <Windows.h>
#include <stdio.h>
/* 预定义 */
#define BUFSIZE 512
void main() { CHAR szVolume[MAX_PATH] = { 0 }; HANDLE hVolume = FindFirstVolumeA(szVolume, MAX_PATH);
if (INVALID_HANDLE_VALUE == hVolume)
return 0;
//string strVolume = szVolume; printf("%s \n", szVolume);
while (FindNextVolumeA(hVolume, szVolume, MAX_PATH))
{
printf("%s \n", szVolume);
GetDirverInfo(szVolume);
} FindVolumeClose(hVolume);//别忘了关闭句柄
system("pause");
} /* ************************************
* BOOL GetDirverInfo(LPSTR szDrive)
* 功能 获取驱动器的属性
* 参数 LPSTR szDrive
* 指明要获取属性的驱动器的根路径 如 C:\
* 返回值 BOOL 是否成功
**************************************/
BOOL GetDirverInfo(LPSTR szDrive)
{
UINT uDriveType;
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD dwFileSystemFlags;
TCHAR szFileSystemNameBuffer[BUFSIZE];
printf("\n%s\n", szDrive);
uDriveType = GetDriveType(szDrive);
// 判断类型
switch (uDriveType)
{
case DRIVE_UNKNOWN:
printf("The drive type cannot be determined. ");
break;
case DRIVE_NO_ROOT_DIR:
printf("The root path is invalid, for example, no volume is mounted at the path. ");
break;
case DRIVE_REMOVABLE:
printf("The drive is a type that has removable media, for example, a floppy drive or removable hard disk. ");
break;
case DRIVE_FIXED:
printf("The drive is a type that cannot be removed, for example, a fixed hard drive. ");
break;
case DRIVE_REMOTE:
printf("The drive is a remote (network) drive. ");
break;
case DRIVE_CDROM:
printf("The drive is a CD-ROM drive. ");
break;
case DRIVE_RAMDISK:
printf("The drive is a RAM disk. ");
break;
default:
break;
}
if (!GetVolumeInformation(
szDrive, NULL, 0,
&dwVolumeSerialNumber,
&dwMaximumComponentLength,
&dwFileSystemFlags,
szFileSystemNameBuffer,
BUFSIZE
))
{
return FALSE;
}
printf("\nVolume Serial Number is %u", dwVolumeSerialNumber);
printf("\nMaximum Component Length is %u", dwMaximumComponentLength);
printf("\nSystem Type is %s\n", szFileSystemNameBuffer); if (dwFileSystemFlags & FILE_SUPPORTS_REPARSE_POINTS)
{
printf("The file system does not support volume mount points.\n");
}
if (dwFileSystemFlags & FILE_VOLUME_QUOTAS)
{
printf("The file system supports disk quotas.\n");
}
if (dwFileSystemFlags & FILE_CASE_SENSITIVE_SEARCH)
{
printf("The file system supports case-sensitive file names.\n");
}
//you can use these value to get more informaion
//
//FILE_CASE_PRESERVED_NAMES
//FILE_CASE_SENSITIVE_SEARCH
//FILE_FILE_COMPRESSION
//FILE_NAMED_STREAMS
//FILE_PERSISTENT_ACLS
//FILE_READ_ONLY_VOLUME
//FILE_SUPPORTS_ENCRYPTION
//FILE_SUPPORTS_OBJECT_IDS
//FILE_SUPPORTS_REPARSE_POINTS
//FILE_SUPPORTS_SPARSE_FILES
//FILE_UNICODE_ON_DISK
//FILE_VOLUME_IS_COMPRESSED
//FILE_VOLUME_QUOTAS
printf("...\n"); return TRUE;
}
\\?\Volume{59f3ebc1-4b4a-4a1c-8f16-887acc647db1}\

\\?\Volume{59f3ebc1-4b4a-4a1c-8f16-887acc647db1}\
The drive is a type that cannot be removed, for example, a fixed hard drive.
Volume Serial Number is 1558048823
Maximum Component Length is 255
System Type is NTFS
The file system does not support volume mount points.
The file system supports disk quotas.
The file system supports case-sensitive file names.
...
\\?\Volume{c7b51b21-70c4-4671-ba87-125fff8a38c0}\ \\?\Volume{c7b51b21-70c4-4671-ba87-125fff8a38c0}\
The drive is a type that cannot be removed, for example, a fixed hard drive.
Volume Serial Number is 4205893873
Maximum Component Length is 255
System Type is NTFS
The file system does not support volume mount points.
The file system supports disk quotas.
The file system supports case-sensitive file names.
...
\\?\Volume{74e82255-bb32-43e8-b69f-8abd4aa6f59f}\ \\?\Volume{74e82255-bb32-43e8-b69f-8abd4aa6f59f}\
The drive is a type that cannot be removed, for example, a fixed hard drive.
Volume Serial Number is 2159020960
Maximum Component Length is 255
System Type is NTFS
The file system does not support volume mount points.
The file system supports disk quotas.
The file system supports case-sensitive file names.
...
\\?\Volume{69606d6b-d3d2-4ff9-9fa5-15fccbad2e75}\ \\?\Volume{69606d6b-d3d2-4ff9-9fa5-15fccbad2e75}\
The drive is a type that cannot be removed, for example, a fixed hard drive.
Volume Serial Number is 1715457383
Maximum Component Length is 255
System Type is NTFS
The file system does not support volume mount points.
The file system supports disk quotas.
The file system supports case-sensitive file names.
...
\\?\Volume{d4498fcb-69ea-4f07-ad08-f3442baf89ce}\ \\?\Volume{d4498fcb-69ea-4f07-ad08-f3442baf89ce}\
The drive is a type that cannot be removed, for example, a fixed hard drive.
Volume Serial Number is 3359563498
Maximum Component Length is 255
System Type is FAT32

Windows程序设计(2) - API-02 文件系统的更多相关文章

  1. Windows API教程文件系统

    本篇文章主要介绍了"Windows API教程文件系统",主要涉及到Windows API教程文件系统方面的内容,对于Windows API教程文件系统感兴趣的同学可以参考一下. ...

  2. Windows 程序设计

    一.Win32 API /******************************************************************** created: 2014/04/1 ...

  3. windows程序设计简介

    大家好,非常高兴和大家一起分享Windows开发心得,Windows已经诞生很多年了,一直因为它的简单易用而深受欢迎,相信很多人在使用Windows的时候,一定有这样一个想法:希望自己将来可以写一个很 ...

  4. Windows程序设计学习笔记(一)Windows内存管理初步

    学习Windows程序设计也有一些时间了,为了记录自己的学习成果,以便以后查看,我希望自己能够坚持写下一系列的学习心得,对自己学习的内容进行总结,同时与大家交流.因为刚学习所以可能有的地方写不不正确, ...

  5. Windows程序设计:格式化对话框的设计

    刚开始学习Windows程序设计,磕磕碰碰,先做个小笔记缓缓神经,主要是将MessageBox这个Windows API函数的. MessageBox函数是许多人刚开始学习Windows程序设计或者是 ...

  6. windows程序设计.第一个windos程序

    Windows程序设计(第5版) windows程序需要调用API. 第一个Windows程序 /*HelloMsg.c -- Displays "Hello World!" in ...

  7. 20145219 《Java程序设计》第02周学习总结

    20145219 <Java程序设计>第02周学习总结 教材学习内容总结 类型:基本类型.类类型(参考类型) 基本类型: 整数:short占2字节,int占4字节,long占8字节 字节: ...

  8. Windows程序设计(1)——Win32运行原理(一)

    CPU保护模式与Windows系统 1 Windows多任务 2 虚拟内存 3 处理器的特权级别 内核对象 1 内核对象有什么用 2 对象句柄 3 使用计数 1 CPU保护模式与Windows系统 8 ...

  9. Windows程序设计(0)——编程之前

    Windows程序设计之前 1 做什么 2 解决什么问题 3 有哪些资源 在开始真正的编程之前,需要了解要做的事情是什么,要解决的解决的问题是什么,有哪些资源可以使用. 1 Windows程序设计之前 ...

  10. windows程序设计01_utf8编码问题

    坚持与妥协 从学程序的第一天老师就给我们说源代码应该使用utf8保存.因为先入为主,"源代码应该使用utf8"的观念已经在"学院派"出身的程序员脑子里根深蒂固. ...

随机推荐

  1. 第二章-数据绑定和第一个AnglarJS Web应用

    Angularjs中的数据绑定 AngularJS创建实时模板来代替视图,而不是将数据合并进模板之后更新DOM.任何一个独立视图组件中的值都是动态替换的.这个功能可以说是AngularJS中最最重要的 ...

  2. uniapp轻轻松松开发各种类型的小程序

    1.前言 现在移动端用户使用量占据了市场大部分的比例,今天 给大家说说怎么去开发一个小程序,今天使用的是uniapp 2.什么是uniapp uni-app 是一个使用 Vue.js 开发所有前端应用 ...

  3. akka-typed(2) - typed-actor交流方式和交流协议

    akka系统是一个分布式的消息驱动系统.akka应用由一群负责不同运算工作的actor组成,每个actor都是被动等待外界的某种消息来驱动自己的作业.所以,通俗点描述:akka应用就是一群actor相 ...

  4. [安卓基础] 003.建立你的第一个App

    创建一个android工程项目 我们使用android提供的集成开发工具(Eclipse+ADT)来创建android工程项目.用这个集成开发工具创建项目,简单,方便,快捷,且自动帮助我们生成基础的文 ...

  5. 泛微 e-cology OA 前台SQL注入漏洞

    0x00概述 该漏洞是由于OA系统的WorkflowCenterTreeData接口在收到用户输入的时候未进行安全过滤,oracle数据库传入恶意SQL语句,导致SQL漏洞. 0x01影响范围 使用o ...

  6. "锁定文件失败 打不开磁盘或它所依赖的某个快照磁盘。模块启动失败。未能启动虚拟机"--解决方法

    今天正在使用kali的时候,电脑突然死机了..强制重启,在进入虚拟机发现报错: "锁定文件失败 打不开磁盘或它所依赖的某个快照磁盘.模块启动失败.未能启动虚拟机." 1.问题起因 ...

  7. 相邻元素margin的自动合并与float的坑

    css中相邻元素的margin其实是会自动合并的,且取较大值. <!DOCTYPE html> <html lang="en"> <head> ...

  8. 团队作业第五次——Alpha冲刺

    这个作业属于哪个课程 软件工程 这个作业要求在哪里 团队作业第五次--Alpha冲刺 这个作业的目标 Alpha冲刺 作业正文 正文 github链接 项目地址 其他参考文献 无 一.代码规范与计划 ...

  9. HTML、CSS与JS实现简易iPhone计算器

    效果如图 源码,通俗易懂 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  10. Redis 入门到分布式 (四) 瑞士军刀Redis其他功能

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 目录: 慢查询 Pipeline 发布订阅 Bitmap(位图) HyperLogLog GEO 一.慢 ...