Hello Everyone
Hello Everyone

Reputation: 329

How to catch an exception when opening a file with CFileDialog fails

I need to catch an exception when the file or files I am going to open is/are being in use or opened by other processes or applications. I am using MFC's CFileDialog's DoModal to open a list of selected files. However, when I click Open after selecting a file in the Open File Dialog, a small popup dialog appears stating that the selected one is being used by another application. DoModal method returns only (either with success or failure code) i.e after the Open File Dialog closes. So I don't know where to insert code to check if the selected file can't be opened when the Open File Dialog is open.

CFileDialog fd (TRUE, NULL, _T("*.pid"), OFN_FILEMUSTEXIST | OFN_HIDEREADONLY);
if (fd.DoModal() == IDOK)
{
    //Get selected filenames 
} 

I would want to insert an AfxMessageBox call when I double click to select a file or click Open button in the OpenFileDialog windows.

Upvotes: 1

Views: 287

Answers (1)

xMRi
xMRi

Reputation: 15385

Use the virtual function CFileDialog::OnShareViolation.

Also you can remove OFN_SHAREAWARE and do your own check with CFileDialog::OnFileNameOK

Upvotes: 3

Related Questions