線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1492
推到 Plurk!
推到 Facebook!

記憶卡已從讀卡機拔起來,但是chdir還是偵測到的問題?

缺席
sunwingman
一般會員


發表:26
回覆:47
積分:14
註冊:2008-05-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-01-22 14:38:55 IP:61.61.xxx.xxx 訂閱
問題大致上是:

當程式第一次執行時,如果沒插記憶卡,chdir會回傳-1,說讀卡機沒插卡,所以沒辦法切換到此目錄
可是當我插上卡,讀取裡面影像之後,再把卡拔掉,程式重頭再run一次時(程式沒關掉,只是重頭在run一次)

雖然我沒插卡,但是chdir竟然回傳0,表示他進入那讀卡機的磁碟目錄下了!
而且即時我把讀卡機的USB連接線拔掉,再重插,一樣會有同樣的問題,除非我的程式關掉再開一次。

所以想請教各位前輩,程式碼片段如下,簡短說明一下:

當程式執行到某地方,會開始DetectCardReaderTimer,然後會偵測DRIVE_REMOVABLE類型的磁碟,
找到之後,就會呼叫DirFiles,這是如果讀卡機插槽內有插卡,chdir會回傳0,表示可以進入此目錄下,如果沒有插卡,則應該會回傳-1。


[code cpp]
void __fastcall TMainFm::DirFiles(AnsiString RootDir, bool chkSubDir, bool incPath)
{
//TODO: Add your source code here

Application->ProcessMessages();
SetErrorMode(SEM_FAILCRITICALERRORS); //不讓錯誤訊息出現
int error=chdir(RootDir.c_str()); // 切換到指定目錄去
if(error == -1)
return;
else
{
//有偵測到插卡就會進來-------------------------------------------------
DetectCardReader->Enabled=false;
CheckCardShow->Interval=3000;
CheckCardShow->Enabled=true;
TSearchRec Sr; // 設Sr為存放檔案資訊的結構變數
AnsiString FileName; // 設定檔案名稱之用的暫存變數

int done = FindFirst("*.*", faDirectory | faAnyFile, Sr);
while(done == 0)
{

if (!(Sr.Attr & faDirectory)) // 如果是檔案
{
if (incPath) // 如果檔名要包含路徑資料在內
{
char curdir[MAXPATH 1];
GetCurrentDirectory(MAXPATH, curdir); // 取得現行目錄名稱
FileName = AnsiString(curdir) "\\" Sr.Name; // 設定含路徑的檔名
}
else
FileName = Sr.Name; // 設定不含路徑的檔名

//Bmp[Img_Count] = new Graphics::TBitmap();
//限定讀取JPG或是BMP圖檔-----------------------------------------------------------------------
if((ExtractFileExt(FileName)==".jpg")||(ExtractFileExt(FileName)==".JPG"))
{
FileList->Add(FileName);
Img_Count ;
TJPEGImage *jp = new TJPEGImage;
try
{
jp->LoadFromFile(FileName);
}
catch(Exception &e)
{
Img_Count--;
FileList->Delete(Img_Count);
}

delete jp;
}

if((ExtractFileExt(FileName)==".bmp")||(ExtractFileExt(FileName)==".BMP"))
{
FileList->Add(FileName);
Img_Count ;
Graphics::TBitmap *t=new Graphics::TBitmap();
try
{
t->LoadFromFile(FileName);
}
catch(Exception &e)
{
Img_Count--;
FileList->Delete(Img_Count);
}
delete t;
}
//----------------------------------------------------------------------------------------------

/*if(Img_Count==6)
{
GoToNext();
CardShow->Enabled=true;
FirstShow=true;
}*/

}
else if(chkSubDir) // 如果要檢查次目錄內的檔案
{
if (Sr.Name != "." && Sr.Name != "..") // 如果不是相對目錄代碼的話
{
DirFiles(Sr.Name.c_str(), true, incPath); // 如果是目錄的話就遞迴處理
chdir(String("..").c_str()); // 切換回前一層目錄
}
}
done = FindNext(Sr); // 找目錄中的下一個檔案

}//end while(done == 0)

FindClose(Sr);
}
}
//----------------------------------------------------------------------------------------------

void __fastcall TMainFm::DetectCardReaderTimer(TObject *Sender)
{
LPTSTR lpBuffer;
DWORD dwDeviceCnt;
DWORD SecPerCls,BytesPerSec,NumOfFreeCls,TotalNumOfCls;
double DiskSpace;
dwDeviceCnt = GetLogicalDriveStrings(0, lpBuffer); //讀到字串的大小

if(dwDeviceCnt)
{
char *buf = new char[dwDeviceCnt];
GetLogicalDriveStrings(dwDeviceCnt, buf);
for(DWORD idx=0; idx<(dwDeviceCnt-1); idx)
{
if(buf[idx]==0)
buf[idx] =' ';
}

AnsiString sDevice=buf;
while(!sDevice.IsEmpty())
{
sDevice.Delete(1,sDevice.Pos(" "));
vStrDevice.push_back(sDevice.SubString(1,sDevice.Pos(" ")-1));
}

AnsiString Dir;
for(unsigned int idx=0; idx {
switch(GetDriveType(vStrDevice[idx].c_str()))
{
case DRIVE_REMOVABLE:

Dir = vStrDevice[idx]; //這個就是可移除的裝置,例如:usb的磁碟機,軟碟機等
bool CheckSubDirectory = true; // 取得檔案名稱時是否要檢查次目錄內的檔案?
bool NameIncludePath = true; // 取得檔案名稱時是否要包含路徑名稱?
DirFiles(Dir, CheckSubDirectory, NameIncludePath);
break;
}
}

delete buf;
vStrDevice.clear();
}

}
//---------------------------------------------------------------------------

[/code]


系統時間:2024-04-20 16:13:07
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!