views:

51

answers:

0

I have the following code to get a valid file name

CFileDialog *MyDlg(new CFileDialog(FALSE,NULL,0,OFN_HIDEREADONLY|OFN_FILEMUSTEXIST,"CSV (*.csv)|*.csv||",NULL));

bool done = false;
CString sFileName = "";
while ( !done ) 
{
    /* (Keep looping until we get a "good" filename) */
    if ( MyDlg->DoModal() != IDOK )
        return;

    sFileName = MyDlg->GetPathName();
    if ( sFileName.Find(".") == -1 ) 
    {
        sFileName += ".csv";
    }

    /* Check if the selected file already exists */
    if ( (_access(sFileName, 0)) != -1 ) 
    {
        /* File already exists - ask for confirmation to overwrite */
        int returnVal = AfxMessageBox("The file: \"" + sFileName + 
            "\" already exists.  Are you sure you want to overwrite this file?",
            MB_YESNO|MB_DEFBUTTON2);

        if (returnVal == IDYES) 
        {
            /* Allow the overwrite */
            done = true;
        }
    } 
    else 
    {
        /* Does not exist - proceed */
        done = true;
    }
}

This code works great on my XP machine as works fine on a Windows Server 2003. However it has a problem on Windows Server 2008 (and I assume Windows7). If the file exists and the user chooses to not overwrite the file I get an error message saying 'Encountered an improper argument' before the dialog is displayed again to choose another location.

Any help would be greatly appreciated.