用什么函数得到所选择文件夹的路径啊?比如说C:/,C:/WINDOWS
用一个编辑框,一个按纽,个一个LIST控件 点按纽出现一个文件选择框,相当于一个CFILE,选择后在EDIT里显示其路径(不是文件的路径,而是文件夹的路径)
怎么实现把该文件夹下的所有文件名(包括子文件夹,比如说选择了C:/那么C:/AA里的文件也要显示出来)在一个LIST控件里显示啊?
小弟初学,请各位前辈指教

解决方案 »

  1.   

    摘录自msdnThis small program recurses every directory on the C:\ drive and prints the name of the directory.#include <afxwin.h>
    #include <iostream>using namespace std;void Recurse(LPCTSTR pstr)
    {
    CFileFind finder;// build a string with wildcards
    CString strWildcard(pstr);
    strWildcard += _T("\\*.*");// start working for files
    BOOL bWorking = finder.FindFile(strWildcard);while (bWorking)
    {
    bWorking = finder.FindNextFile();// skip . and .. files; otherwise, we'd
    // recur infinitely!if (finder.IsDots())
    continue;// if it's a directory, recursively search itif (finder.IsDirectory())
    {
    CString str = finder.GetFilePath();
    cout << (LPCTSTR) str << endl;
    Recurse(str);
    }
    }finder.Close();
    }void main()
    {
    if (!AfxWinInit(GetModuleHandle(NULL), NULL, GetCommandLine(), 0))
    cout << "panic!" << endl;
    else
    Recurse(_T("C:"));
    }
      

  2.   

    得到path以后插入listctrl用
    m_list.InsertColumn( 0, "ID", LVCFMT_LEFT, 40 );//插入列
    int nRow = m_list.InsertItem(0, path);//插入行
    m_list.SetItemText(nRow, 1, strtemp);//设置每项数据具体的函数参数请参考msdn
      

  3.   

    ////////////////////////////////////////////////////////////////////////////
    // CFileTreeView viewclass CFileTreeView : public CView
    {
    protected:
    CFileTreeView();           // protected constructor used by dynamic creation
    DECLARE_DYNCREATE(CFileTreeView)// Attributes
    public:// Operations
    public:
    void OnFiletreeRefresh();
    void HideButton(HTREEITEM hti);
    BOOL HaveSubDirOrFile(char *szCurDir);
    BOOL GetSubSiblingItem(HTREEITEM hItem, char *szCurDir);
    CString GetFileExName(TCHAR cFileName[MAX_PATH]);
    void DisplayButton(HTREEITEM hti);
    void InitRootDirectory();
    CTreeCtrl m_tree;
    CImageList m_ImageList;
    CString m_strOpenFile;// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CFileTreeView)
    public:
    protected:
    virtual void OnDraw(CDC* pDC);      // overridden to draw this view
    virtual void OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView);
    //}}AFX_VIRTUAL// Implementation
    protected:
    virtual ~CFileTreeView();
    #ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
    #endif // Generated message map functions
    protected:
    //{{AFX_MSG(CFileTreeView)
    afx_msg void OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult);
    afx_msg void OnDblclk(NMHDR* pNMHDR, LRESULT* pResult);
    afx_msg void OnSize(UINT nType, int cx, int cy);
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };
      

  4.   

    上面是用TREE控件,供参考,个人认为用树状的话,自目录和父目录分的比较清晰:
    void CFileTreeView::InitRootDirectory()
    {
    HTREEITEM hItem,hti;
    CStringArray arDriverName;
    CString strDriverName;
    CString str;
    UINT nDriverType; if (m_tree.GetImageList(TVSIL_NORMAL) == NULL)
    m_tree.SetImageList(&m_ImageList,TVSIL_NORMAL); hItem = m_tree.InsertItem(_T("&Icirc;&Ograve;&micro;&Auml;&micro;&ccedil;&Auml;&Ocirc;"),0,0,TVI_ROOT,TVI_LAST); char szDriverName[500];
    char szDriverLabel[500];
    memset(szDriverName,0,sizeof(szDriverName));
    memset(szDriverLabel,0,sizeof(szDriverLabel));
    DWORD nLength = GetLogicalDriveStrings(sizeof(szDriverName),szDriverName);

    for (int i=0; i<(int)nLength; i++)
    {
    if (szDriverName[i] != '\0')
    strDriverName += szDriverName[i];
    else
    {
    strDriverName = strDriverName.Left(strDriverName.GetLength() - 1);
    arDriverName.Add(strDriverName);
    strDriverName = "";
    }
    } char *szCurDir = NULL;
    for (i=0; i<arDriverName.GetSize(); i++)
    {
    nDriverType = GetDriveType((LPCTSTR)arDriverName.GetAt(i));
    GetVolumeInformation((LPCTSTR)(arDriverName.GetAt(i) + "\\"),szDriverLabel,\
    sizeof(szDriverLabel),NULL,NULL,0,NULL,0);
    hti = m_tree.InsertItem((CString)szDriverLabel + "(" + arDriverName.GetAt(i) + ")",\
    nDriverType,nDriverType,hItem,TVI_LAST); DisplayButton(hti);
    memset(szDriverLabel,0,sizeof(szDriverLabel));
    } hti = m_tree.GetRootItem();
    m_tree.SelectItem(hti);
    m_tree.Expand(hti,TVE_EXPAND);
    }void CFileTreeView::DisplayButton(HTREEITEM hti)
    {
    TVITEM tvi;
    tvi.mask = TVIF_CHILDREN;
    tvi.hItem = hti;
    tvi.cChildren = 1; m_tree.SetItem(&tvi);
    }CString CFileTreeView::GetFileExName(TCHAR cFileName[MAX_PATH])
    {
    CString strFileName,strFileExName; strFileName = (CString)cFileName;
    strFileExName.Empty();
    for (int i=strFileName.GetLength()-1; i>=0; i--)
    {
    if (strFileName.GetAt(i) != '.')
    strFileExName = strFileName.GetAt(i) + strFileExName;
    else
    return strFileExName;
    }
    return strFileExName;
    }BOOL CFileTreeView::GetSubSiblingItem(HTREEITEM hItem, char *szCurDir)
    {
    BOOL bHaveChild = FALSE;
    CString strEx;
    HANDLE hFind;
    HTREEITEM hti;
    WIN32_FIND_DATA wfd; if (_chdir(szCurDir) != 0) return bHaveChild;
    hFind = FindFirstFile(_T("*.*"),&wfd);
    if(hFind == INVALID_HANDLE_VALUE) return bHaveChild; if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) &&
    !(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
    {
    if( wfd.cFileName[0] != '.' )
    {
    bHaveChild = TRUE;
    hti = m_tree.InsertItem(wfd.cFileName,6,6,hItem,TVI_FIRST);
    m_tree.SetItemData(hti,DIR);
    DisplayButton(hti);
    _chdir(".."); // &sup2;é&Otilde;&Ograve;&Iacute;ê±&Iuml;&Ouml;&reg;&ordm;ó, ·&micro;&raquo;&Oslash;&Eacute;&Iuml;&Ograve;&raquo;&frac14;&para;&Auml;&iquest;&Acirc;&frac14;


    else

    strEx = GetFileExName(wfd.cFileName);
    strEx.MakeUpper();
    if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
    {
    if (strEx == "RTF")
    {
    hti = m_tree.InsertItem(wfd.cFileName,8,8,hItem,TVI_LAST);
    m_tree.SetItemData(hti,FILE);
    bHaveChild = TRUE;
    }
    else if (strEx == "TXT" || strEx == "LOG" || strEx == "INI" || strEx == "HTM" ||
     strEx == "HTML"|| strEx == "CPP" || strEx == "H"   || strEx == "JAVA"||
     strEx == "C"   || strEx == "CSS" || strEx == "DIC" || strEx == "INF")
    {
    hti = m_tree.InsertItem(wfd.cFileName,7,7,hItem,TVI_LAST);
    m_tree.SetItemData(hti,FILE);
    bHaveChild = TRUE;
    }
    }
    }  while(FindNextFile(hFind, &wfd)) 

    if ((wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && 
    !(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))

    if(wfd.cFileName[0] != '.') 

    bHaveChild = TRUE;
    hti = m_tree.InsertItem(wfd.cFileName,6,6,hItem,TVI_FIRST);
    m_tree.SetItemData(hti,DIR);
    DisplayButton(hti);
    _chdir(".."); // &sup2;é&Otilde;&Ograve;&Iacute;ê±&Iuml;&Ouml;&reg;&ordm;ó, ·&micro;&raquo;&Oslash;&Eacute;&Iuml;&Ograve;&raquo;&frac14;&para;&Auml;&iquest;&Acirc;&frac14; 


    else 

    strEx = GetFileExName(wfd.cFileName);
    strEx.MakeUpper();
    if (!(wfd.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN))
    {
    if (strEx == "RTF")
    {
    hti = m_tree.InsertItem(wfd.cFileName,8,8,hItem,TVI_LAST);
    m_tree.SetItemData(hti,FILE);
    bHaveChild = TRUE;
    }
    else if (strEx == "TXT" || strEx == "LOG" || strEx == "INI" || strEx == "HTM" ||
     strEx == "HTML"|| strEx == "CPP" || strEx == "H"   || strEx == "JAVA"||
     strEx == "C"   || strEx == "CSS" || strEx == "DIC" || strEx == "INF")
    {
    hti = m_tree.InsertItem(wfd.cFileName,7,7,hItem,TVI_LAST);
    m_tree.SetItemData(hti,FILE);
    bHaveChild = TRUE;
    }
    }


    FindClose(hFind);
    return bHaveChild;}BOOL CFileTreeView::HaveSubDirOrFile(char *szCurDir)
    {
    BOOL bHaveChild = FALSE;
    HANDLE hFind;
    WIN32_FIND_DATA wfd;
    CString strEx; if (_chdir(szCurDir) != 0) return bHaveChild;
    hFind = FindFirstFile(_T("*.*"),&wfd);
    if(hFind == INVALID_HANDLE_VALUE) return bHaveChild; if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 

    if( wfd.cFileName[0] != '.' ) 
    {
    bHaveChild = TRUE;
    _chdir(".."); // &sup2;é&Otilde;&Ograve;&Iacute;ê±&Iuml;&Ouml;&reg;&ordm;ó, ·&micro;&raquo;&Oslash;&Eacute;&Iuml;&Ograve;&raquo;&frac14;&para;&Auml;&iquest;&Acirc;&frac14; 
    FindClose(hFind);
    return bHaveChild;


    else

    strEx = GetFileExName(wfd.cFileName);
    strEx.MakeUpper();
    if (strEx == "TXT" || strEx == "LOG" || strEx == "INI" || strEx == "HTM" ||
        strEx == "HTML"|| strEx == "CPP" || strEx == "H"   || strEx == "JAVA"||
        strEx == "C"   || strEx == "CSS" || strEx == "DIC" || strEx == "INF" ||
    strEx == "RTF")
    {
    bHaveChild = TRUE;
    FindClose(hFind);
    return bHaveChild;
    }
    }  while(FindNextFile(hFind, &wfd)) 

    if(wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 

    if(wfd.cFileName[0] != '.') 

    bHaveChild = TRUE;
    _chdir("..");
    FindClose(hFind);
    return bHaveChild;


    else 

    strEx = GetFileExName(wfd.cFileName);
    strEx.MakeUpper();
    if (strEx == "TXT" || strEx == "LOG" || strEx == "INI" || strEx == "HTM" ||
    strEx == "HTML"|| strEx == "CPP" || strEx == "H"   || strEx == "JAVA"||
    strEx == "C"   || strEx == "CSS" || strEx == "DIC" || strEx == "INF" ||
    strEx == "RTF")
    {
    bHaveChild = TRUE;
    FindClose(hFind);
    return bHaveChild;
    }
    }

    FindClose(hFind);
    return bHaveChild;
    }
      

  5.   

    void CFileTreeView::HideButton(HTREEITEM hti)
    {
    TVITEM tvi;
    tvi.mask = TVIF_CHILDREN;
    tvi.hItem = hti;
    tvi.cChildren = 0; m_tree.SetItem(&tvi);
    }void CFileTreeView::OnActivateView(BOOL bActivate, CView* pActivateView, CView* pDeactiveView) 
    { CView::OnActivateView(bActivate, pActivateView, pDeactiveView);
    }void CFileTreeView::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    HTREEITEM hti,hParent;
    CTreeCtrl &tree = m_tree;
    CString strPath,str1,str2; hti = tree.GetSelectedItem();
    if (tree.GetItemData(hti) == FILE)
    {
    strPath = tree.GetItemText(hti);
    hParent = tree.GetParentItem(hti);
    while (hParent != tree.GetRootItem())
    {
    strPath = tree.GetItemText(hParent) + "\\" + strPath;
    hParent = tree.GetParentItem(hParent);
    }
    strPath += "\\";
    str1 = strPath.Mid(strPath.Find(":") - 2 + 1,2);
    str2 = strPath.Right(strPath.GetLength() - strPath.Find(":") - 2);
    strPath = str1 + str2;
    strPath = strPath.Left(strPath.GetLength() - 1);
    m_strOpenFile = strPath; /////////////////////////////////////////////////////////////////////
    //&Aacute;&Ugrave;&Ecirc;±&acute;ò&iquest;&ordf;&Icirc;&Auml;&frac14;&thorn;
    // CMDIChildWnd *pMainFrame = ((CMainFrame*)GetParentFrame())->MDIGetActive();
    // CWnd *pWnd = ((CRichFrame*)pMainFrame)->m_wndSplitter.GetPane(0,0);
    // CRichSyntaxView *pRich = (CRichSyntaxView*)pWnd;
    // pRich->SetFileName(strPath);
    // pRich->OnOpen();
    /////////////////////////////////////////////////////////////////////
    } *pResult = 0;
    }////////////////////////////////////////////////////////////////////////
    //&micro;±&ETH;è&Ograve;&ordf;&Otilde;&sup1;&iquest;&ordf;&frac12;&Uacute;&micro;&atilde;&Ecirc;±&pound;&not;&frac12;&laquo;&cedil;&Atilde;&frac12;&Uacute;&micro;&atilde;&Iuml;&Acirc;&micro;&Auml;&Auml;&Uacute;&Egrave;&Yacute;&frac14;&Oacute;&Egrave;&euml;
    ////////////////////////////////////////////////////////////////////////
    void CFileTreeView::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult) 
    {
    NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;

    CTreeCtrl &tree = m_tree;
    HTREEITEM hti = pNMTreeView->itemNew.hItem;

    if (hti != tree.GetRootItem())
    {
    //&Ccedil;&aring;&iquest;&Otilde;&para;ù×&Oacute;
    HTREEITEM hChild = tree.GetChildItem(hti);
    while (hChild)
    {
    tree.DeleteItem(hChild);
    hChild = tree.GetChildItem(hti);
    } char *szCurDir;
    HTREEITEM hParent;
    CString strPath,str1,str2; strPath = tree.GetItemText(hti);
    hParent = tree.GetParentItem(hti);
    while (hParent != tree.GetRootItem())
    {
    strPath = tree.GetItemText(hParent) + "\\" + strPath;
    hParent = tree.GetParentItem(hParent);
    }
    strPath += "\\";
    str1 = strPath.Mid(strPath.Find(":") - 2 + 1,2);
    str2 = strPath.Right(strPath.GetLength() - strPath.Find(":") - 2);
    strPath = str1 + str2; szCurDir = (LPSTR)(LPCSTR)strPath;
    if (!GetSubSiblingItem(hti,szCurDir))
    HideButton(hti);
    } *pResult = 0;
    }void CFileTreeView::OnSize(UINT nType, int cx, int cy) 
    {
    CView::OnSize(nType, cx, cy);

    if (m_tree)
    m_tree.MoveWindow(0,0,cx,cy,TRUE);
    }int CFileTreeView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
    {
    if (CView::OnCreate(lpCreateStruct) == -1)
    return -1;

    if (!m_tree)
    m_tree.Create(WS_CHILD|WS_VISIBLE|TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS,CRect(0,0,0,0),this,ID_FILETREE); if (!m_ImageList)
    m_ImageList.Create(IDB_FILE,16,16,RGB(0,255,0)); InitRootDirectory(); return 0;
    }void CFileTreeView::OnFiletreeRefresh()
    {
    InitRootDirectory();
    }
      

  6.   

    选择路径可以用,CFileDialog.用Findfirstfile,FindNextfile来查找文件,自己把他们添加进去。
      

  7.   

    选择文件夹路径:
    void CMainFrame::OnBrowse() 
    {
    CString str;
    BROWSEINFO bi;
    char name[MAX_PATH];
    ZeroMemory(&bi,sizeof(BROWSEINFO));
    bi.hwndOwner=GetSafeHwnd();
    bi.pszDisplayName=name;
    bi.lpszTitle=_T("Browse Folder");
    bi.ulFlags=BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
    LPITEMIDLIST idl=SHBrowseForFolder(&bi);
    if(idl==NULL)
    return;
    SHGetPathFromIDList(idl,str.GetBuffer(MAX_PATH));
    str.ReleaseBuffer();
    CString path=str;
    if(str.GetAt(str.GetLength()-1)!='\\')
    path+="\\";
            return path;
    }
      

  8.   

    sun1126() :
    可以得到路径名了,谢谢你了,怎么让它在EDIT里面显示呢?我把EDIT跟一个变量m_edit绑定了,可是用m_edit=path; UPDATEDATA();无法显示。
    然后就是最后一个问题了,怎么把路径下的所有文件的文件名都显示在LIST里面呢
      

  9.   

    http://www.vckbase.com/code/downcode.asp?id=2328一个可以指定初始目录的目录选择对话框程序 
      

  10.   

    路径是可以得到了呀,但是怎么得到该路径下的所有文件名呢?我不会用CFileFind类,再帮我看看哦
      

  11.   

    呵呵.最近刚刚写的.void CPList::ReadDirectory(CHAR *path)
    {
    static WIN32_FIND_DATA fData;
    int OldLength = (int)strlen(path);
    HANDLE H;
    strcat(path, "*.*");
    H = FindFirstFile(path, &fData);
    path[OldLength] = '\0';
    do{
    if(fData.cFileName[0] == '.')
    continue;
    if(fData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
    {
    strcat(path, fData.cFileName);
    strcat(path, "\\");
    ReadDirectory(path);
    path[OldLength] = '\0';
    }
    else
    {
    strcat(path, fData.cFileName);
    InsertItem(m_nTotal,path, 0);
    path[OldLength] = '\0';
    }
    }while(FindNextFile(H, &fData));
    }
      

  12.   

    谢谢大家了。 Rainday_Ye() 跟我的需求是一样的。虽然你们的代码跟我需要的有点不同,但是我还是从中学到了很多。