Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to Select Directory throught File Open dialog

How to Select Directory throught File Open dialog

Scheduled Pinned Locked Moved C / C++ / MFC
comtutorial
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GauranG Shah
    wrote on last edited by
    #1

    Hello Friends, In one of my application I need to let use to select the directory. I am using File Open dialog for that. but that Only let user select File. Is there any Way I can use this to select the Directory This is the code I am using.It Allows me to select *.exe File When Choose EXE in Files of Type in File Open Dialog. but Not able to select the Directoy when Select FOLDER Option.

    void FileOpenDlg(WCHAR *Path)
    {
    OPENFILENAME ofn;

    	ofn.lStructSize = sizeof ( OPENFILENAME) ;
    	ofn.hwndOwner = hDlg ;
    	ofn.hInstance = NULL ;	//Not supported
    	**ofn.lpstrFilter =\_T("Application (\*.Exe;\*.avi) \\0 \*.Exe;\*.avi\\0 FOLDER \\0 ") ;** 
    	ofn.lpstrCustomFilter = NULL ;	//Not Supported
    	ofn.nMaxCustFilter = 0 ;	//Not Supported
    	ofn.nFilterIndex = 1 ;
    	ofn.lpstrFile = Path ;		// Set in Open and Close functions
    	ofn.nMaxFile = MAX\_PATH ;
    	ofn.lpstrFileTitle = NULL ;	// buffer that receives the file name (without path information) 
    	ofn.nMaxFileTitle = MAX\_PATH ;
    	ofn.lpstrInitialDir = NULL;
    	ofn.lpstrTitle = NULL ;
    	ofn.Flags = 0 ; 	       // Set in Open and Close functions
    	ofn.nFileOffset = 0 ;
    	ofn.nFileExtension = 0 ;
    	ofn.lpstrDefExt = NULL ;
    	ofn.lCustData = 0L ;
    	ofn.lpfnHook = NULL ;		//Not Supported
    	ofn.lpTemplateName = NULL ;	//Not Supported
    	GetOpenFileName(&ofn);
    

    }

    [ Screen Capture ][ Tool Tip ]

    N A J 3 Replies Last reply
    0
    • G GauranG Shah

      Hello Friends, In one of my application I need to let use to select the directory. I am using File Open dialog for that. but that Only let user select File. Is there any Way I can use this to select the Directory This is the code I am using.It Allows me to select *.exe File When Choose EXE in Files of Type in File Open Dialog. but Not able to select the Directoy when Select FOLDER Option.

      void FileOpenDlg(WCHAR *Path)
      {
      OPENFILENAME ofn;

      	ofn.lStructSize = sizeof ( OPENFILENAME) ;
      	ofn.hwndOwner = hDlg ;
      	ofn.hInstance = NULL ;	//Not supported
      	**ofn.lpstrFilter =\_T("Application (\*.Exe;\*.avi) \\0 \*.Exe;\*.avi\\0 FOLDER \\0 ") ;** 
      	ofn.lpstrCustomFilter = NULL ;	//Not Supported
      	ofn.nMaxCustFilter = 0 ;	//Not Supported
      	ofn.nFilterIndex = 1 ;
      	ofn.lpstrFile = Path ;		// Set in Open and Close functions
      	ofn.nMaxFile = MAX\_PATH ;
      	ofn.lpstrFileTitle = NULL ;	// buffer that receives the file name (without path information) 
      	ofn.nMaxFileTitle = MAX\_PATH ;
      	ofn.lpstrInitialDir = NULL;
      	ofn.lpstrTitle = NULL ;
      	ofn.Flags = 0 ; 	       // Set in Open and Close functions
      	ofn.nFileOffset = 0 ;
      	ofn.nFileExtension = 0 ;
      	ofn.lpstrDefExt = NULL ;
      	ofn.lCustData = 0L ;
      	ofn.lpfnHook = NULL ;		//Not Supported
      	ofn.lpTemplateName = NULL ;	//Not Supported
      	GetOpenFileName(&ofn);
      

      }

      [ Screen Capture ][ Tool Tip ]

      N Offline
      N Offline
      Nishad S
      wrote on last edited by
      #2

      Have a look[^]

      - NS -

      1 Reply Last reply
      0
      • G GauranG Shah

        Hello Friends, In one of my application I need to let use to select the directory. I am using File Open dialog for that. but that Only let user select File. Is there any Way I can use this to select the Directory This is the code I am using.It Allows me to select *.exe File When Choose EXE in Files of Type in File Open Dialog. but Not able to select the Directoy when Select FOLDER Option.

        void FileOpenDlg(WCHAR *Path)
        {
        OPENFILENAME ofn;

        	ofn.lStructSize = sizeof ( OPENFILENAME) ;
        	ofn.hwndOwner = hDlg ;
        	ofn.hInstance = NULL ;	//Not supported
        	**ofn.lpstrFilter =\_T("Application (\*.Exe;\*.avi) \\0 \*.Exe;\*.avi\\0 FOLDER \\0 ") ;** 
        	ofn.lpstrCustomFilter = NULL ;	//Not Supported
        	ofn.nMaxCustFilter = 0 ;	//Not Supported
        	ofn.nFilterIndex = 1 ;
        	ofn.lpstrFile = Path ;		// Set in Open and Close functions
        	ofn.nMaxFile = MAX\_PATH ;
        	ofn.lpstrFileTitle = NULL ;	// buffer that receives the file name (without path information) 
        	ofn.nMaxFileTitle = MAX\_PATH ;
        	ofn.lpstrInitialDir = NULL;
        	ofn.lpstrTitle = NULL ;
        	ofn.Flags = 0 ; 	       // Set in Open and Close functions
        	ofn.nFileOffset = 0 ;
        	ofn.nFileExtension = 0 ;
        	ofn.lpstrDefExt = NULL ;
        	ofn.lCustData = 0L ;
        	ofn.lpfnHook = NULL ;		//Not Supported
        	ofn.lpTemplateName = NULL ;	//Not Supported
        	GetOpenFileName(&ofn);
        

        }

        [ Screen Capture ][ Tool Tip ]

        A Offline
        A Offline
        Anurag Gandhi
        wrote on last edited by
        #3

        Use the followign code:

        CString YourClassName::GetFolderName()
        {
        BROWSEINFO br;

        CString title = "Select the folder to convert its files";
        char buff\[MAX\_PATH\];
        
        br.hwndOwner = m\_hWnd;
        br.pidlRoot = NULL;
        br.lParam = NULL;
        br.lpfn = NULL;
        br.lpszTitle =( (LPTSTR) ((LPCTSTR)title));
        br.pszDisplayName =(LPTSTR) buff;
        //br.ulFlags = BIF\_BROWSEINCLUDEFILES;
        br.ulFlags = BIF\_RETURNONLYFSDIRS;
        
        LPITEMIDLIST item;
        item = ::SHBrowseForFolder ( &br );
        ::SHGetPathFromIDList ( item, buff );
        
        return buff;
        

        }

        Hoping that it will help you.

        Anurag Gandhi. http://www.softgandhi.co.nr Life is a computer program and every one is the programmer of his own life.

        J 1 Reply Last reply
        0
        • A Anurag Gandhi

          Use the followign code:

          CString YourClassName::GetFolderName()
          {
          BROWSEINFO br;

          CString title = "Select the folder to convert its files";
          char buff\[MAX\_PATH\];
          
          br.hwndOwner = m\_hWnd;
          br.pidlRoot = NULL;
          br.lParam = NULL;
          br.lpfn = NULL;
          br.lpszTitle =( (LPTSTR) ((LPCTSTR)title));
          br.pszDisplayName =(LPTSTR) buff;
          //br.ulFlags = BIF\_BROWSEINCLUDEFILES;
          br.ulFlags = BIF\_RETURNONLYFSDIRS;
          
          LPITEMIDLIST item;
          item = ::SHBrowseForFolder ( &br );
          ::SHGetPathFromIDList ( item, buff );
          
          return buff;
          

          }

          Hoping that it will help you.

          Anurag Gandhi. http://www.softgandhi.co.nr Life is a computer program and every one is the programmer of his own life.

          J Offline
          J Offline
          JudyL_MD
          wrote on last edited by
          #4

          Anurag Gandhi wrote:

          LPITEMIDLIST item; item = ::SHBrowseForFolder ( &br ); ::SHGetPathFromIDList ( item, buff ); return buff;

          You need to release the memory allocated for item after you get the path name. As the documentation for SHBrowseForFolder says: The calling application is responsible for freeing the returned PIDL by using the Shell allocator's IMalloc::Free method. To retrieve a handle to that IMalloc interface, call SHGetMalloc.

          LPMALLOC pMalloc;
          
          SHGetMalloc (&pMalloc);
          pMalloc->Free (item);
          pMalloc->Release ();
          

          Judy

          A 1 Reply Last reply
          0
          • G GauranG Shah

            Hello Friends, In one of my application I need to let use to select the directory. I am using File Open dialog for that. but that Only let user select File. Is there any Way I can use this to select the Directory This is the code I am using.It Allows me to select *.exe File When Choose EXE in Files of Type in File Open Dialog. but Not able to select the Directoy when Select FOLDER Option.

            void FileOpenDlg(WCHAR *Path)
            {
            OPENFILENAME ofn;

            	ofn.lStructSize = sizeof ( OPENFILENAME) ;
            	ofn.hwndOwner = hDlg ;
            	ofn.hInstance = NULL ;	//Not supported
            	**ofn.lpstrFilter =\_T("Application (\*.Exe;\*.avi) \\0 \*.Exe;\*.avi\\0 FOLDER \\0 ") ;** 
            	ofn.lpstrCustomFilter = NULL ;	//Not Supported
            	ofn.nMaxCustFilter = 0 ;	//Not Supported
            	ofn.nFilterIndex = 1 ;
            	ofn.lpstrFile = Path ;		// Set in Open and Close functions
            	ofn.nMaxFile = MAX\_PATH ;
            	ofn.lpstrFileTitle = NULL ;	// buffer that receives the file name (without path information) 
            	ofn.nMaxFileTitle = MAX\_PATH ;
            	ofn.lpstrInitialDir = NULL;
            	ofn.lpstrTitle = NULL ;
            	ofn.Flags = 0 ; 	       // Set in Open and Close functions
            	ofn.nFileOffset = 0 ;
            	ofn.nFileExtension = 0 ;
            	ofn.lpstrDefExt = NULL ;
            	ofn.lCustData = 0L ;
            	ofn.lpfnHook = NULL ;		//Not Supported
            	ofn.lpTemplateName = NULL ;	//Not Supported
            	GetOpenFileName(&ofn);
            

            }

            [ Screen Capture ][ Tool Tip ]

            J Offline
            J Offline
            JudyL_MD
            wrote on last edited by
            #5

            Note my reply to the code that Anurag gave you. Also, prior to using that code, you must have called CoInitialize or CoInitializeEx. Judy

            1 Reply Last reply
            0
            • J JudyL_MD

              Anurag Gandhi wrote:

              LPITEMIDLIST item; item = ::SHBrowseForFolder ( &br ); ::SHGetPathFromIDList ( item, buff ); return buff;

              You need to release the memory allocated for item after you get the path name. As the documentation for SHBrowseForFolder says: The calling application is responsible for freeing the returned PIDL by using the Shell allocator's IMalloc::Free method. To retrieve a handle to that IMalloc interface, call SHGetMalloc.

              LPMALLOC pMalloc;
              
              SHGetMalloc (&pMalloc);
              pMalloc->Free (item);
              pMalloc->Release ();
              

              Judy

              A Offline
              A Offline
              Anurag Gandhi
              wrote on last edited by
              #6

              Thanks for the correction Judy. :)

              Anurag Gandhi. http://www.softgandhi.co.nr Life is a computer program and every one is the programmer of his own life.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups