TDriveComboBox和TDirectoryListBox一起使用时出现的访问光驱或软驱错误

Lilyhappy 2003-04-19 04:38:41
当TDriveComboBox和TDirectoryListBox一起使用时,有函数
void __fastcall TFormOut::DriveComboBox1Change(TObject *Sender)
{
DirectoryListBox1->Drive=DriveComboBox1->Drive;
}
此函数的作用是当DriveComboBox1中的驱动器改变时,使DirectoryListBox1也随之改变。
在运行时,如果DriveComboBox1中的驱动器选择的是硬盘驱动器的时候,程序正常运行。
但如果选择的是光驱或软驱,而这时光驱或软驱中没有磁盘的话,程序就会出错而停止运行。
应该如何解决这一问题?
是否能通过编程实现事先判断到DriveComboBox1中选择的是光驱或软驱,再判断里面有没有磁盘,如果没有磁盘的话提示插入磁盘?
...全文
136 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdsq 2003-04-21
  • 打赏
  • 举报
回复
同意搂上的.
jishiping 2003-04-20
  • 打赏
  • 举报
回复
在IDE环境里仍然会出现系统的错误,但是不在IDE里执行(比如在资源管理器里执行程序),
你就看到效果了。凡是异常,在IDE里总是可以看到错误信息的。
Lilyhappy 2003-04-20
  • 打赏
  • 举报
回复
jishiping(JSP 季世平) 的方法已经试过了,不行啊
jishiping 2003-04-20
  • 打赏
  • 举报
回复
void __fastcall TForm1::DriveComboBox1Change(TObject *Sender)
{
try {
DirectoryListBox1->Drive=DriveComboBox1->Drive;
}
catch(...) {
ShowMessage("没有磁盘,请插入磁盘");
DriveComboBox1->Drive = DirectoryListBox1->Drive;
}
}
chifengwatch 2003-04-19
  • 打赏
  • 举报
回复
//cbPath是TComboBox,mInfo是TMemo
void __fastcall TForm1::FormCreate(TObject *Sender)
{
DWORD dwSize;
char *lpDrives, *lpItem;

//Determine how big a buffer is needed to hold all the logical device paths
dwSize = GetLogicalDriveStrings(0, NULL);
//Initialize a buffer of the proper size
lpDrives = new char[dwSize];
//Get the logical drives
GetLogicalDriveStrings(dwSize, lpDrives);
//Initialize a working variable
lpItem = lpDrives;
//While there are more items in the list
//(end of list is indicated by a 0 length string
while (strlen(lpItem) != 0)
{
//Add the current item to the path list
cbPath->Items->Add(lpItem);
//Increment the pointer to the next path item in the list
lpItem += strlen(lpItem) + 1;
}

//If there are some logical device paths
if (cbPath->Items->Count)
{
//Make the first item the default
cbPath->ItemIndex = 0;
//Load the drive info for the default device path
GetDriveInfo();
}

//Free the memory allocated for the device paths
delete [] lpDrives;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::cbPathChange(TObject *Sender)
{
//User changed the combo box selection. Update the display
GetDriveInfo();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::GetDriveInfo(void)
{
UINT Type;
String s;
char lpVolumeName[255], lpFileSystemName[100], szBuffer[512];
DWORD dwVolumeSerialNumber, dwMaximumComponentLength, dwFileSystemFlags;

//Clear the info display
mInfo->Clear();

//Get the drive type
Type = GetDriveType(cbPath->Items->Strings[cbPath->ItemIndex].c_str());
//Update the display accoring to the drive type
switch (Type)
{
case 0:
s = "Unknown";
break;
case 1:
s = "Does not exist";
break;
case DRIVE_REMOVABLE:
s = "Removable Disk Drive";
break;
case DRIVE_FIXED:
s = "Fixed Disk Drive";
break;
case DRIVE_REMOTE:
s = "Network Drive";
break;
case DRIVE_CDROM:
s = "CD ROM Drive";
break;
case DRIVE_RAMDISK:
s = "RAM Disk";
}

mInfo->Lines->Add("Device Type: " + s);

//Cjeck the volume information. Returns TRUE on success
if (GetVolumeInformation(cbPath->Items->Strings[cbPath->ItemIndex].c_str(),
lpVolumeName,
255,
&dwVolumeSerialNumber,
&dwMaximumComponentLength,
&dwFileSystemFlags,
lpFileSystemName,
100))
{
//Update the display with the information
wsprintf(szBuffer, "Volume Name: %s", lpVolumeName);
mInfo->Lines->Add(szBuffer);
wsprintf(szBuffer, "Serial Number: %u", dwVolumeSerialNumber);
mInfo->Lines->Add(szBuffer);
wsprintf(szBuffer, "File System: %s", lpFileSystemName);
mInfo->Lines->Add(szBuffer);
}
//GetVolumeInformation failed
else
//No disk in removable drive or drive not ready
mInfo->Lines->Add("DRIVE IS NOT READY");

13,873

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧