CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
(图)邪恶的韩国UMPC 使用 Java 编写数据库应用新规范
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  基础类

谁知道在vc里怎么获取一个文件夹路径

楼主ivenher9527(爱饭盒)2005-04-08 15:20:40 在 VC/MFC / 基础类 提问

谁知道在vc里怎么获取一个文件夹路径   ,SHGetSpecialFolderLocation     SHBrowseForFolder怎么用的? 问题点数:10、回复次数:15Top

1 楼wuchi(风云)回复于 2005-04-08 15:30:27 得分 0

CFileDialog,  
  CfileTop

2 楼ivenher9527(爱饭盒)回复于 2005-04-08 15:38:06 得分 0

CFileDialog  
  不是只能用来打开,保存一个文件吗,我想怎么选择到一个文件夹,不是文件Top

3 楼qqwwing(草莓猪)回复于 2005-04-08 17:19:03 得分 0

有一个现成的类可以,不过是非模式的对话框,有时候不适合的  
   
                    CString   m_strFolder;  
   
  if(g_fSelectFolderDlg(&m_strFolder,m_strFolder,false))  
  {  
  MessageBox(m_strFolder);  
  }  
  路径就在m_strFolder中,记得包含头文件   #include   "global.h"Top

4 楼ivenher9527(爱饭盒)回复于 2005-04-08 17:38:12 得分 0

找不到   "global.h"   文件Top

5 楼huangyonghy321(我爱JAVA)回复于 2005-04-08 17:46:18 得分 0

利用CFileDialog类  
  如下  
  CString   path;  
  CFileDialog   dlg(TRUE,NULL,"*.*");  
  if   (dlg.DoModal()==IDOK){//当你找到文件并确定打开时  
  path   =   dlg.GetPathName();  
   
  }Top

6 楼chenjunge(狄克)回复于 2005-04-08 17:51:10 得分 0

shlobj.hTop

7 楼ivenher9527(爱饭盒)回复于 2005-04-08 17:52:24 得分 0

我想要的是一个能够选择文件夹的方法,就像安装一个程序时,你可以选择一个文件夹作为路径Top

8 楼ivenher9527(爱饭盒)回复于 2005-04-08 17:54:38 得分 0

不在这里边定义的   shlobj.h  
  Top

9 楼ivenher9527(爱饭盒)回复于 2005-04-11 14:44:54 得分 0

怎么没人来看看啊,Top

10 楼qiuxiangyong(qxy)回复于 2005-04-11 15:00:25 得分 0

当然是用SHBrowseForFolder  
  BROWSEINFO   info;  
  memset(&info,0);  
   
  SHBrowseForFolder(info);  
   
  大概就是这个样子啦!Top

11 楼xqk(夏乾坤)回复于 2005-04-11 15:31:16 得分 10

我用的是现成的类的方法  
   
  //dirdialog.h  
  ////////////////////////////////////////////////////////////////////////  
  //   DirDialog.h:   interface   for   the   CDirDialog   class.  
  //////////////////////////////////////////////////////////////////////  
   
  #if   !defined(AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_)  
  #define   AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_  
   
  #if   _MSC_VER   >=   1000  
  #pragma   once  
  #endif   //   _MSC_VER   >=   1000  
   
  class   CDirDialog  
  {  
  public:  
   
          CDirDialog();  
          virtual   ~CDirDialog();  
   
          BOOL   DoBrowse(CWnd   *pwndParent   =   NULL);  
   
          CString   m_strWindowTitle;  
          CString   m_strPath;  
          CString   m_strInitDir;  
          CString   m_strSelDir;  
          CString   m_strTitle;  
          int     m_iImageIndex;  
          BOOL   m_bStatus;  
   
  private:  
   
          virtual   BOOL   SelChanged(LPCSTR   /*   lpcsSelection   */,   CString&   /*   csStatusText   */)   {   return   TRUE;   };  
          static   int   __stdcall   CDirDialog::BrowseCtrlCallback(HWND   hwnd,   UINT   uMsg,   LPARAM   lParam,   LPARAM   lpData);  
  };  
   
  #endif   //   !defined(AFX_DIRDIALOG_H__62FFAC92_1DEE_11D1_B87A_0060979CDF6D__INCLUDED_)  
   
  Top

12 楼xqk(夏乾坤)回复于 2005-04-11 15:32:17 得分 0

//dirdialog.cpp  
   
  ///////////////////////////////////////////////////////////////////////////  
  //   DirDialog.cpp:   implementation   of   the   CDirDialog   class.  
  //////////////////////////////////////////////////////////////////////  
   
  #include   "stdafx.h"  
  #include   "DirDialog.h"  
  #include   "shlobj.h"  
   
  #ifdef   _DEBUG  
  #undef   THIS_FILE  
  static   char   THIS_FILE[]=__FILE__;  
  #define   new   DEBUG_NEW  
  #endif  
   
  //   Callback   function   called   by   SHBrowseForFolder's   browse   control  
  //   after   initialization   and   when   selection   changes  
  int   __stdcall   CDirDialog::BrowseCtrlCallback(HWND   hwnd,   UINT   uMsg,   LPARAM   lParam,   LPARAM   lpData)  
  {  
          CDirDialog*   pDirDialogObj   =   (CDirDialog*)lpData;  
          if   (uMsg   ==   BFFM_INITIALIZED   )  
          {  
                  if(   !   pDirDialogObj->m_strSelDir.IsEmpty()   )  
                          ::SendMessage(hwnd,   BFFM_SETSELECTION,   TRUE,   (LPARAM)(LPCTSTR)(pDirDialogObj->m_strSelDir));  
                  if(   !   pDirDialogObj->m_strWindowTitle.IsEmpty()   )  
                          ::SetWindowText(hwnd,   (LPCTSTR)   pDirDialogObj->m_strWindowTitle);  
          }  
          else   if(   uMsg   ==   BFFM_SELCHANGED   )  
          {  
                  LPITEMIDLIST   pidl   =   (LPITEMIDLIST)   lParam;  
                  char   selection[MAX_PATH];  
                  if(   !   ::SHGetPathFromIDList(pidl,   selection)   )  
                          selection[0]   =   '\0';  
   
                  CString   csStatusText;  
                  BOOL   bOk   =   pDirDialogObj->SelChanged(selection,   csStatusText);  
   
                  if(   pDirDialogObj->m_bStatus   )  
                          ::SendMessage(hwnd,   BFFM_SETSTATUSTEXT   ,   0,   (LPARAM)(LPCSTR)csStatusText);  
   
                  ::SendMessage(hwnd,   BFFM_ENABLEOK,   0,   bOk);  
          }  
      return   0;  
  }  
   
  //////////////////////////////////////////////////////////////////////  
  //   Construction/Destruction  
  //////////////////////////////////////////////////////////////////////  
   
  CDirDialog::CDirDialog()  
  {  
  m_bStatus   =   FALSE;  
  }  
   
  CDirDialog::~CDirDialog()  
  {  
  }  
   
  BOOL   CDirDialog::DoBrowse(CWnd   *pwndParent)  
  {  
   
          if(   !   m_strSelDir.IsEmpty()   )  
          {  
                  m_strSelDir.TrimRight();  
                  if(   m_strSelDir.Right(1)   ==   "\\"   ||   m_strSelDir.Right(1)   ==   "//"   )  
                          m_strSelDir   =   m_strSelDir.Left(m_strSelDir.GetLength()   -   1);  
          }  
   
          LPMALLOC   pMalloc;  
          if   (SHGetMalloc   (&pMalloc)!=   NOERROR)  
                  return   FALSE;  
   
          BROWSEINFO   bInfo;  
          LPITEMIDLIST   pidl;  
          ZeroMemory   (   (PVOID)   &bInfo,sizeof   (BROWSEINFO));  
   
          if   (!m_strInitDir.IsEmpty   ())  
          {  
                  OLECHAR               olePath[MAX_PATH];  
                  ULONG                   chEaten;  
                  ULONG                   dwAttributes;  
                  HRESULT               hr;  
                  LPSHELLFOLDER   pDesktopFolder;  
                  //  
                  //   Get   a   pointer   to   the   Desktop's   IShellFolder   interface.  
                  //  
                  if   (SUCCEEDED(SHGetDesktopFolder(&pDesktopFolder)))  
                  {  
                          //  
                          //   IShellFolder::ParseDisplayName   requires   the   file   name   be   in   Unicode.  
                          //  
                          MultiByteToWideChar(CP_ACP,   MB_PRECOMPOSED,   m_strInitDir.GetBuffer(MAX_PATH),   -1,  
                                                                  olePath,   MAX_PATH);  
   
                          m_strInitDir.ReleaseBuffer   (-1);  
                          //  
                          //   Convert   the   path   to   an   ITEMIDLIST.  
                          //  
                          hr   =   pDesktopFolder->ParseDisplayName(NULL,  
                                                                                                  NULL,  
                                                                                                  olePath,  
                                                                                                  &chEaten,  
                                                                                                  &pidl,  
                                                                                                  &dwAttributes);  
                          if   (FAILED(hr))  
                          {  
                                  pMalloc   ->Free   (pidl);  
                                  pMalloc   ->Release   ();  
                                  return   FALSE;  
                          }  
                          bInfo.pidlRoot   =   pidl;  
   
                  }  
          }  
          bInfo.hwndOwner   =   pwndParent   ==   NULL   ?   NULL   :   pwndParent->GetSafeHwnd();  
          bInfo.pszDisplayName   =   m_strPath.GetBuffer   (MAX_PATH);  
          bInfo.lpszTitle   =   (m_strTitle.IsEmpty())   ?   "Open"   :   m_strTitle;  
          bInfo.ulFlags   =   BIF_RETURNFSANCESTORS  
                                          |   BIF_RETURNONLYFSDIRS  
                                          |   (m_bStatus   ?   BIF_STATUSTEXT   :   0);  
   
          bInfo.lpfn   =   BrowseCtrlCallback;     //   address   of   callback   function  
          bInfo.lParam   =   (LPARAM)this;             //   pass   address   of   object   to   callback   function  
   
          if   ((pidl   =   ::SHBrowseForFolder(&bInfo))   ==   NULL)  
          {  
                  return   FALSE;  
          }  
          m_strPath.ReleaseBuffer();  
          m_iImageIndex   =   bInfo.iImage;  
   
          if   (::SHGetPathFromIDList(pidl,   m_strPath.GetBuffer(MAX_PATH))   ==   FALSE)  
          {  
                  pMalloc   ->Free(pidl);  
                  pMalloc   ->Release();  
                  return   FALSE;  
          }  
   
          m_strPath.ReleaseBuffer();  
   
          pMalloc   ->Free(pidl);  
          pMalloc   ->Release();  
   
          return   TRUE;  
  }Top

13 楼xqk(夏乾坤)回复于 2005-04-11 15:34:30 得分 0

//调用  
   
        CDirDialog   dlgDir;  
   
        dlgDir.m_strTitle   =   _T("Select   directory   for   file   search");  
        UpdateData(TRUE);  
        dlgDir.m_strSelDir   =   "C:\\";  
        dlgDir.m_strWindowTitle   =   _T("Select   directory");  
        if   (dlgDir.DoBrowse(this)   ==   IDOK)   {  
              m_Edit1   =   dlgDir.m_strPath;                     //将文件路径写到编辑控件中  
              //   Append   backslash   if   necessary  
              UpdateData(FALSE);  
        }  
  Top

14 楼vBin(彬)回复于 2005-04-11 15:51:28 得分 0

BROWSEINFO   bi   =   {0};  
          LPITEMIDLIST   pidl;  
          IMalloc   *pMalloc   =   NULL;  
          char   cszPath[MAX_PATH];  
   
  bi.hwndOwner   =   m_hWnd;  
  bi.lpszTitle   =   "Title";  
  bi.ulFlags   =   BIF_RETURNONLYFSDIRS;  
  pidl   =   SHBrowseForFolder(&bi);  
  if   (pidl)  
  {  
  if   (SHGetPathFromIDList(pidl,   cszPath))  
  {  
   
          //   Get   Dir  
  }  
   
  if   (SHGetMalloc(&pMalloc))  
  {  
  pMalloc->Free(pidl);  
  pMalloc->Release();  
  }  
  }Top

15 楼ivenher9527(爱饭盒)回复于 2005-04-11 16:30:33 得分 0

谢谢各位,怎么结帖Top

相关问题

  • vc里怎么获取一个文件夹路径
  • 怎样获取ShellTreeView1的文件夹名(含路径)?
  • 获取文件夹下的文件 把前面路径去掉
  • 简单问题:VC中如何创建指定的文件夹路径
  • VC中如何获得某指定路径下的文件夹列表?
  • 用CFileDialog(或其子类)实现获取文件夹的路径,我是说:。。。。容易欧!
  • 怎么得到文件夹的路径?
  • 如何从文件夹路径得到PIDL???
  • 如何取得文件夹的路径
  • 请问在VC中若要在某路径下新建一个文件夹的代码怎么写?多谢!

关键词

  • 文件夹
  • 文件
  • cdirdialog
  • 路径
  • dirdialog
  • strfolder
  • cstring
  • cfiledialog
  • dlg
  • 类

得分解答快速导航

  • 帖主:ivenher9527
  • xqk

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
惹火投票。。火热进行中...

社区焦点:

教你怎样用C#搞笑整人
最懒惰的程序员写的Cache
程序员如何掌握专业英语
Java栈与堆
分享:让人懊恼的面试
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo