You are here:

C++/Selecting Data Directory


Dear Sir
Actually CFileDialog Class provides way to Browse the Directory Structure of your System, and Only allows you to select any file. After selecting file you can get pointers to other files in same directory.

Let Suppose if I have a folder named "ABC", which contains several files. Now i wana to just allow user to select this folder, so that when user click folder through mouse, the OPEN Button in CFileDialog Converts to SELECT and only path of selected folder is returned.

My SECOND Question is that how to set file type Attribute in CFileDialog to restrict user to select specific file.

Also how to pass pointer of one class into an other class constructor.
Thanks in Advance
kahsif gul kazi

Followup To
Question -
Dear sir
first of all thanx 4 ur help regarding my last few Question.
my Q is

I used CFileDialog to select file by browsing  the Directory Structure.

Q1:  Now i wana to implement Dialog which allows to select some Directory (Folder),and return complete path of selected directory.?
Also how i would get  pointer to files in selected directory?

Q2        Also i created a 2nd dialog in dialog based application. Now how to pass pointer of ApplicationNameDlg class to newly created Dialog class Dialog2Dlg( If pointer is passed in Constructor then how to use this pointer in new dialog class).

and friendly any link to any good tutorial regrding using win32 API. and threading Model.

Thanks in Advance
Kashif Gul Kazi

Answer -
Kashif Gul Kazi , Thank you for your question.

1. I am not sure I understand what you are asking. Have you read the documentation for the CFileDialog class? It contains functions such as 'GetNextPathName', which returns the next selected filename.

2. This question mystifies me completely. Unfortunately, you have not given enough information for me to be able to answer your question. Please resubmit your question with more details or a clearer description and I will do my best to answer.

David Spector

kahsif gul kazi,

I assume you do not wish to allow the user to select any files, only a folder.

I believe the user can select a folder icon from the CFileDialog dialog box. Then you can get the pathname of the folder by calling the 'GetPathName' member function.

I believe that restricting the user to selecting certain file types is not easy to do with the GetPathName class. I recommend that you call the Windows '' function instead.

Here is some source code to show one way to solve your problem. IMPORTANT NOTE: This Windows code may not compile because it is dependent on other parts of my personal application library. It should be sufficient to give you some ideas; I cannot supply you with all of the source code needed to make this example actually work. Note the failure of to provide for indenting source code.

If I have not answered your questions, I regret that I have no more time to spend on this. I suggest that you post this question at, which is more advanced than

//   ----------------------------------------------------------------------------
//   Name:         function BrowseForFile
//   Description:   Display the standard file-browsing dialog box so the user
//          can select a file. Store the initial path, if any, in Path;
//          BrowseForFile stores the user choice in Path, or an empty
//          string if the user clicked Cancel. The size of Path must
//          be at least MAX_PATH bytes. Displays the Open dialog if
//          DefaultExt is NULL; otherwise displays the Save dialog.
//          DefaultExt does not include period. Specify DefaultExt for
//          Save File, NULL for Open File. InitialDir may be NULL to
//          indicate the system default (usually the current dir).
//          Changes current dir to that of the path.
//          Filter strings: The first NUL-terminated string in each
//          pair is a display string that describes the filter (for
//          example, "Text Files"), and the second string specifies the
//          filter pattern (for example, "*.txt"). To specify multiple
//          filter patterns for a single display string, use a
//          semicolon to separate the patterns (for example,
//          "*.txt;*.doc;*.bak"). a filter pattern can be a combination
//          of valid filename characters and the asterisk (*) wildcard
//          character. Filter can be NULL. Filter Example:
//          "Images (*.jpg;*.gif)\0*.jpg;*.gif\0All Files\0*.*\0"
//   ----------------------------------------------------------------------------
EXPORT char *      BrowseForFile(HWND ParentWnd, PCSTR Caption,
         PCSTR Filter, PCSTR DefaultExt,
         PCSTR InitialDir, char * Path)
  char         InitialDirPath[MAX_PATH];
  BOOL         Status;

  if (InitialDir)
     lstrcpy(InitialDirPath, InitialDir);
     *InitialDirPath = 0;
     //GetCurrentDirectory(MAX_PATH, InitialDirPath);

  memset(&Info, 0, sizeof(Info));
  Info.lStructSize = sizeof(OPENFILENAME);
  Info.hwndOwner = ParentWnd;
  Info.lpstrFilter = Filter;
  Info.lpstrTitle = Caption;
  Info.lpstrDefExt = DefaultExt;
  Info.lpstrFile = Path;
  Info.nMaxFile = MAX_PATH;
  Info.lpstrInitialDir = InitialDirPath;

  if (DefaultExt)
     Status = GetSaveFileName(&Info);
     Status = GetOpenFileName(&Info);
  if (!Status)
     *Path = 0;

  //TRACE("Path=%s Initial Dir=%s", Path, InitialDirPath);

  //// Allow main dlg to be repainted after Save/Restore dlg
  return Path;
  } // BrowseForFile  


All Answers

Answers by Expert:

Ask Experts


David Spector


Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.


I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2017 All rights reserved.