荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: Second (石开), 信区: Program
标  题: 如 何 得 到 并 修 改 各 驱 动 器 的 信 息
发信站: 荔园晨风BBS站 (Wed Jun  6 07:23:14 2001), 转信

在 设 计 和 文 件 输 入/ 输 出 有 关 的 应 用 程 序 时, 我 们 很 可 能 在
输 入/ 输 出 文 件 前, 需 要 了 解 一 下 源 驱 动 器 或 者 目 标 驱 动 器 的
 各 项 信 息, 比 如 是 否 有 磁 盘 在 软 驱 中, 它 是 否 已 打 开 写 保 护
, 以 及 现 有 磁 盘 的 容 量 等。 遗 憾 的 是,MFC 类 库 中 没 有 提 供 支
持 这 些 功 能 的 类, 所 以 我 们 只 能 通 过Win32 提 供 的 函 数 来 完 成
我 们 的 要 求。 下 面, 我 根 据 自 己 的 编 程 实 践, 通 过 几 段 程 序,
 来 说 明 如 何 利 用Win32 提 供 的 函 数 实 现 对 驱 动 器 的 操 作。 读 者
 可 以 根 据 自 己 的 需 要, 把 介 绍 的 函 数 稍 加 修 改 后, 即 可 插 入
 到 自 己 设 计 的 应 用 程 序 中 去。
下 面 程 序 的 功 能 是 搜 索 计 算 机 中 所 有 驱 动 器, 选 择 出 其 中 软
 盘 驱 动 器 的 驱 动 器 号, 依 次 加 入 到 一 个 下 拉 列 表 框 中。
void FindDriverInfo()
{
CComboBox* Driver=(CComboBox*)GetDlgItem(IDC_DRIVER);
DWORD dwNumBytesForDriveStrings;
HANDLE hHeap;
LPSTR lp;
CString strLogdrive;
int nNumDrives=0, nDriveNum;
dwNumBytesForDriveStrings=GetLogicalDriveStrings(0,NULL)
*sizeof(TCHAR);//实际存储驱动器号的字符串长度
if (dwNumBytesForDriveStrings!=0) {
hHeap=GetProcessHeap();
lp=(LPSTR)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,
dwNumBytesForDriveStrings);//
GetLogicalDriveStrings(HeapSize(hHeap,0,lp),lp);
StringBox.SetSize(dwNumBytesForDriveStrings/sizeof(TCHAR)+1);
while (*lp!=0) {
if (GetDriveType(lp)==DRIVE_REMOVABLE){
Driver->AddString(lp);
StringBox[nNumDrives]=lp;
nNumDrives++;
}
lp=_tcschr(lp,0)+1;
}
}
else AfxMessageBox("Can't Use The Function GetLogicalDriveStrings!");
}
下 面 介 绍 的EmptyDiskSpace() 函 数 主 要 负 责 清 空 指 定 驱 动 器 中 的
磁 盘, 同 时 它 还 负 责 记 录 指 定 驱 动 器 中 磁 盘 的 容 量, 并 得 到
该 磁 盘 的 序 列 号。 在 该 函 数 中, 还 将 调 用 第 七 部 分 提 到 的PreR
emoveDirectory() 函 数, 来 完 成 清 空 工 作。
BOOL EmptyDiskSpace(CString Driver)
{
BOOL result=TRUE;
DWORDSectorsPerCluster; // address of sectors per cluster
DWORDBytesPerSector; // address of bytes per sector
DWORDNumberOfFreeClusters; // address of number of free clusters
DWORDTotalNumberOfClusters;
DWORDTotalBytes;
DWORDFreeBytes;
int  bContinue=1;
char DiskVolumeSerialNumber[30];
//存储驱动器内当前磁盘的序列号
LPCTSTRlpRootPathName;
// address of root directory of the file system
LPTSTRlpVolumeNameBuffer=new char[12];
// address of name of the volume
DWORDnVolumeNameSize=12;
// length of lpVolumeNameBuffer
DWORD VolumeSerialNumber;
// address of volume serial number
DWORD MaximumComponentLength;
// address of system's maximum filename length
DWORD FileSystemFlags;
// address of file system flags
LPTSTRlpFileSystemNameBuffer=new char[10];
// address of name of file system
DWORDnFileSystemNameSize=10;
// length of lpFileSystemNameBuffer
lpRootPathName=Driver;
while (1){
if (GetDiskFreeSpace(Driver, &SectorsPerCluster,
&BytesPerSector, &NumberOfFreeClusters,
&TotalNumberOfClusters))
{//驱动器中有磁盘
TotalBytes=SectorsPerCluster*BytesPerSector
*TotalNumberOfClusters;//磁盘总容量
FreeBytes=SectorsPerCluster*BytesPerSector
*NumberOfFreeClusters;//磁盘空闲空间容量
GetVolumeInformation(lpRootPathName,
lpVolumeNameBuffer, nVolumeNameSize,
&VolumeSerialNumber,
&MaximumComponentLength,
&FileSystemFlags,
lpFileSystemNameBuffer, nFileSystemNameSize);
sprintf(DiskVolumeSerialNumber,"%X",VolumeSerialNumber);
//得到驱动器内当前磁盘的序列号
SetmTotalBytes(TotalBytes/1024);//存储指定驱动器中磁盘的容量
if (TotalBytes!=FreeBytes){//当磁盘总容量不等于空闲空间容量时,
应该执行清空操作
while (bContinue) {
if ((bContinue==2)||(MessageBox
("在驱动器 "+m_Driver+"中的磁盘尚存有数据.
\n您愿意让系统为您删除它们吗?",
"提问",MB_YESNO|MB_ICONQUESTION)==IDYES))
if (!PreRemoveDirectory(Driver))//无法执行清空操作
if (MessageBox("因某种原因系统无法删除
在驱动器 "+m_Driver+"中的磁盘上的数据.
\n请检查磁盘是否没有关闭写保护.
\n您愿意再试一次吗?",
"问题",MB_YESNO|MB_ICONERROR)==IDYES) {
bContinue=2;
continue;
}
else {
bContinue=0;
result=FALSE;
}
else {
MessageBox("成功删除磁盘上的数据!",
"提示信息",MB_OK|MB_ICONINFORMATION);
bContinue=0;
result=TRUE;
}
else {//THE FIRST IF'S ELSE
bContinue=0;
result=FALSE;
}
}
}
else result=TRUE;
break;
}
else {
if (MessageBox("没有磁盘在驱动器 "+m_Driver+"中.
\n您愿意插入一张磁盘再来一次吗?",
"问题",MB_YESNO|MB_ICONASTERISK)==IDYES) continue;
else break;
}
}//END OF WHILE
return result;
}
在MS-DOS 和Windows95 中, 磁 盘 卷 标 最 多 由11 个 字 符 组 成, 并 且 字 母
 的 大 小 写 不 加 区 分。 当 需 要 设 定 指 定 驱 动 器 中 磁 盘 的 卷 标 时
, 只 要 调 用Win32 的SetVolumeLabel() 函 数 即 可, 并 在 第 一 个 参 数 中
 指 明 磁 盘 所 在 的 驱 动 器 号, 在 第 二 个 参 数 中 指 明 新 的 卷 标 号
。 例 如,SetVolumeLabel(DriverNum, NewVolumeLabel)。

--
                            既然热爱生命
                            那么,
                            一切都在意料之中。

※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 192.168.28.190]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店