获取文件属性

来源:互联网 发布:java se 库存管理系统 编辑:程序博客网 时间:2024/05/14 08:02

首先需要创建一个新类:CFolderDialog

//////////////////////////////////////////////////////////////////////////////* DESCRIPTION:CFolderDialog  - Folder Selection Dialog ClassCopyright(C) Armen Hakobyan, 2002 - 2005http://www.codeproject.com/dialog/cfolderdialog.aspVERSION HISTORY:24 Mar 2002 - First release30 Mar 2003 - Some minor changes- Added missing in old Platform SDK new flag definitions  - Added support for both MFC 6.0 and 7.0- Added OnIUnknown handler for Windows XP folder filtration- Added SetExpanded and SetOKText and GetSelectedFolder functions24 May 2003 - Added OnSelChanged implementation14 Jul 2003 - Added custom filtration for Windows XP, thanks to Arik Poznanski29 Nov 2003 - Added SetRootFolder, thanks to Eckhard Schwabe ( and Jose Insa )02 Jan 2004 - Added GetRootFolder, uncomment if needed15 Feb 2005 - Small bug fix in DoModal, thanks to WindSeven*//////////////////////////////////////////////////////////////////////////////#ifndef __FOLDERDLG_H__#define __FOLDERDLG_H__#if defined( _MSC_VER ) && ( _MSC_VER >= 1020 )#pragma once#endif/////////////////////////////////////////////////////////////////////////////#ifndef __AFXDLGS_H__#include < AfxDlgs.h >#endif#ifndef __ATLCONV_H__#include < AtlConv.h >// MBCS/Unicode Conversion Macros#endif// Uncomment if using GetRootFolder//#ifndef _INC_SHLWAPI//#include < shlwapi.h >//#endif//#pragma comment( lib, "shlwapi.lib" )/////////////////////////////////////////////////////////////////////////////#ifndefSAFE_DELETE2#define SAFE_DELETE2( p ) \if( p ){ delete[] p; p = NULL; }#endif#ifndefSAFE_ZEROMEMORY#define SAFE_ZEROMEMORY( p, size ) \if( p ){ ZeroMemory( p, size ); }#endif#ifndefSAFE_RELEASE#if defined( __cplusplus )#define SAFE_RELEASE( p ) \if( p ){ p->Release(); p = NULL; }#else#define SAFE_RELEASE( p ) \if( p ){ p->lpVtbl->Release( p ); p = NULL; }#endif#endif#ifndefSAFE_COTASKMEMFREE#define SAFE_COTASKMEMFREE( p ) \if( p ){ CoTaskMemFree( (LPVOID)p ); p = NULL; }#endif#ifndef_countof#define _countof( x ) \( sizeof( x ) / sizeof( x[ 0 ] ) )#endif/////////////////////////////////////////////////////////////////////////////#ifndef BFFM_SETOKTEXT// Version 5.0 or later#define BFFM_SETOKTEXT( WM_USER + 105 )// Unicode only, req. BIF_USENEWUI#define BFFM_SETEXPANDED( WM_USER + 106 )// Unicode only, req. BIF_USENEWUI#endif#ifndef BIF_NEWDIALOGSTYLE// Version 5.0 or later#define BIF_NEWDIALOGSTYLE0x0040#define BIF_BROWSEINCLUDEURLS0x0080#define BIF_UAHINT0x0100// Req. BIF_NEWDIALOGSTYLE#define BIF_NONEWFOLDERBUTTON0x0200#define BIF_NOTRANSLATETARGETS0x0400#define BIF_SHAREABLE0x8000// Req. BIF_USENEWUI#define BIF_USENEWUI( BIF_NEWDIALOGSTYLE | BIF_EDITBOX )#endif/////////////////////////////////////////////////////////////////////////////class CFolderDialog : public CCommonDialog{DECLARE_DYNAMIC( CFolderDialog )public:CFolderDialog(IN LPCTSTRpszTitle= NULL, IN LPCTSTRpszSelPath= NULL,IN CWnd*pWndParent= NULL,IN UINTuFlags= BIF_RETURNONLYFSDIRS );virtual ~CFolderDialog( VOID );public:#if ( _MFC_VER >= 0x0700 )// VC++ 2002 (7.0)virtual INT_PTRDoModal( VOID );#elsevirtual INTDoModal( VOID );#endifBOOLSetRootFolder( IN LPCTSTR pszPath );BOOLGetRootFolder( IN OUT LPTSTR pszPath );BOOLSetSelectedFolder( IN LPCTSTR pszPath );public:AFX_INLINE LPCTSTRGetFolderPath( VOID )  const;AFX_INLINE LPCTSTRGetFolderName( VOID )  const;AFX_INLINE INTGetFolderImage( VOID ) const;AFX_INLINE LPCTSTRGetSelectedFolder( VOID ) const;AFX_INLINE BROWSEINFO& GetBI( VOID );AFX_INLINE const BROWSEINFO& GetBI( VOID ) const;protected:BROWSEINFOm_bi;TCHARm_szSelPath[ MAX_PATH ];TCHARm_szFolPath[ MAX_PATH ];protected:DECLARE_MESSAGE_MAP()protected: // Overridablesvirtual VOID OnInitialized( VOID );virtual VOID OnSelChanged( IN LPITEMIDLIST  pItemIDList );virtual INT OnValidateFailed( IN LPCTSTR /*pszPath*/ );protected: // Windows XP or latervirtual VOID OnIUnknown( IN IUnknown* /*pIUnknown*/ );protected: // Valid to call only from the above handlersVOID EnableOK( IN BOOL bEnable = TRUE );VOID SetSelection( IN LPITEMIDLIST pItemIDList );VOID SetSelection( IN LPCTSTR pszPath );VOID SetStatusText( IN LPCTSTR pszText );protected: // Shell version 5.0 or later:VOID SetExpanded( IN LPCTSTR pszPath );VOID SetExpanded( IN LPITEMIDLIST pItemIDList );VOID SetOKText( IN LPCTSTR pszText );private:HWND m_hWnd; // used only in the callback functionprivate:static INT CALLBACK BrowseCallbackProc(IN HWND hWnd, IN UINT uMsg, IN LPARAM lParam, IN LPARAM lpData);};/////////////////////////////////////////////////////////////////////////////AFX_INLINE LPCTSTR CFolderDialog::GetSelectedFolder( VOID ) const{ return m_szSelPath; }AFX_INLINE BROWSEINFO& CFolderDialog::GetBI( VOID ){ return m_bi; }AFX_INLINE const BROWSEINFO& CFolderDialog::GetBI( VOID ) const{ return m_bi; }/////////////////////////////////////////////////////////////////////////////// Filled after a call to DoModalAFX_INLINE LPCTSTR CFolderDialog::GetFolderPath( VOID ) const{ return m_szFolPath; }AFX_INLINE LPCTSTR CFolderDialog::GetFolderName( VOID ) const{ return m_bi.pszDisplayName; }AFX_INLINE INT CFolderDialog::GetFolderImage( VOID ) const{ return m_bi.iImage; }/////////////////////////////////////////////////////////////////////////////#endif // __FOLDERDLG_H__/////////////////////////////////////////////////////////////////////////////
.CPP文件
//////////////////////////////////////////////////////////////////////////////* DESCRIPTION:CFolderDialog  - Folder Selection Dialog ClassCopyright(C) Armen Hakobyan, 2002 - 2005http://www.codeproject.com/dialog/cfolderdialog.aspVERSION HISTORY:24 Mar 2002 - First release30 Mar 2003 - Some minor changes- Added missing in old Platform SDK new flag definitions  - Added support for both MFC 6.0 and 7.0- Added OnIUnknown handler for Windows XP folder filtration- Added SetExpanded and SetOKText and GetSelectedFolder functions24 May 2003 - Added OnSelChanged implementation14 Jul 2003 - Added custom filtration for Windows XP, thanks to Arik Poznanski29 Nov 2003 - Added SetRootFolder, thanks to Eckhard Schwabe ( and Jose Insa )02 Jan 2004 - Added GetRootFolder, uncomment if needed15 Feb 2005 - Small bug fix in DoModal, thanks to WindSeven*//////////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include "FolderDlg.h"/////////////////////////////////////////////////////////////////////////////#ifndef BFFM_VALIDATEFAILED#ifndef UNICODE#define BFFM_VALIDATEFAILED3#else#define BFFM_VALIDATEFAILED4#endif#endif#ifndef BFFM_IUNKNOWN#define BFFM_IUNKNOWN5#endif/////////////////////////////////////////////////////////////////////////////// CFolderDialogIMPLEMENT_DYNAMIC( CFolderDialog, CDialog )CFolderDialog::CFolderDialog( IN LPCTSTR pszTitle /*NULL*/,  IN LPCTSTR pszSelPath /*NULL*/,  IN CWnd* pWndParent /*NULL*/,  IN UINT uFlags /*BIF_RETURNONLYFSDIRS*/ ) : CCommonDialog( pWndParent ) , m_hWnd( NULL ){::ZeroMemory( &m_bi, sizeof( BROWSEINFO ) );::ZeroMemory( m_szFolPath, MAX_PATH );::ZeroMemory( m_szSelPath, MAX_PATH );// Fill datam_bi.hwndOwner= pWndParent->GetSafeHwnd();m_bi.pidlRoot= NULL;m_bi.lpszTitle= pszTitle;m_bi.ulFlags= uFlags;m_bi.lpfn= (BFFCALLBACK)&BrowseCallbackProc;m_bi.lParam= (LPARAM)this;// The size of this buffer is assumed to be MAX_PATH bytes:m_bi.pszDisplayName = new TCHAR[ MAX_PATH ];ASSERT( m_bi.pszDisplayName != NULL );SAFE_ZEROMEMORY( m_bi.pszDisplayName, ( MAX_PATH * sizeof( TCHAR ) ));if( pszSelPath )SetSelectedFolder( pszSelPath );}CFolderDialog::~CFolderDialog( VOID ){SAFE_COTASKMEMFREE( m_bi.pidlRoot );SAFE_DELETE2( m_bi.pszDisplayName );::ZeroMemory( &m_bi, sizeof( BROWSEINFO ) );}BEGIN_MESSAGE_MAP( CFolderDialog, CCommonDialog )END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CFolderDialog message handlers// SetRootFolder By Jose Insa// Microsoft knowledge Base Article// ID Q132750: Convert a File Path to an ITEMIDLISTBOOL CFolderDialog::SetRootFolder( IN LPCTSTR pszPath ){ASSERT_VALID( this );if( !pszPath ){SAFE_COTASKMEMFREE( m_bi.pidlRoot );return TRUE;}ASSERT( AfxIsValidString( pszPath, MAX_PATH ) );HRESULT  hResult  = S_FALSE;IShellFolder* pDeskFolder = NULL;hResult = ::SHGetDesktopFolder( &pDeskFolder );if ( hResult == S_OK ){LPITEMIDLIST pidlRoot = NULL;LPTSTR       pszRoot  = const_cast< LPTSTR >( pszPath );// Convert the path to an ITEMIDLIST:USES_CONVERSION;hResult = pDeskFolder->ParseDisplayName(NULL, NULL, T2W( pszRoot ), NULL, &pidlRoot, NULL );if( hResult == S_OK ){SAFE_COTASKMEMFREE( m_bi.pidlRoot );m_bi.pidlRoot = pidlRoot;}SAFE_RELEASE( pDeskFolder );}return ( hResult == S_OK );} // NOTE: pszPath buffer must be at least// MAX_PATH characters in size:BOOL CFolderDialog::GetRootFolder( IN OUT LPTSTR pszPath ){ASSERT_VALID( this );ASSERT( AfxIsValidString( pszPath, MAX_PATH ) );return ::SHGetPathFromIDList( m_bi.pidlRoot, pszPath );} BOOL CFolderDialog::SetSelectedFolder( IN LPCTSTR pszPath ){ASSERT( AfxIsValidString( pszPath, MAX_PATH ) );return (BOOL)::lstrcpy( m_szSelPath, pszPath );}/////////////////////////////////////////////////////////////////////////////#if ( _MFC_VER >= 0x0700 )INT_PTR CFolderDialog::DoModal( VOID )#elseINTCFolderDialog::DoModal( VOID )#endif{ASSERT_VALID( this );ASSERT( m_bi.lpfn != NULL );INT_PTR nRet = -1;m_bi.hwndOwner = PreModal();LPITEMIDLIST pItemIDList = ::SHBrowseForFolder( &m_bi );if( pItemIDList ){if( ::SHGetPathFromIDList( pItemIDList, m_szFolPath ) )nRet = IDOK;SAFE_COTASKMEMFREE( pItemIDList );}elsenRet = IDCANCEL;PostModal();return ( nRet );}/////////////////////////////////////////////////////////////////////////////// Overridables:VOID CFolderDialog::OnInitialized( VOID ){if( ::lstrlen( m_szSelPath ) > 0 )SetSelection( m_szSelPath );}VOID CFolderDialog::OnSelChanged( IN LPITEMIDLIST pItemIDList ){if( m_bi.ulFlags & BIF_STATUSTEXT ){TCHAR szSelFol[ MAX_PATH ] = { 0 };if( ::SHGetPathFromIDList( pItemIDList, szSelFol ) )SetStatusText( szSelFol );}}INT CFolderDialog::OnValidateFailed( IN LPCTSTR /*pszPath*/ ){::MessageBeep( MB_ICONHAND );return 1; // Return 1 to leave dialog open, 0 - to end one}VOID CFolderDialog::OnIUnknown( IN IUnknown* /*pIUnknown*/ ){}/////////////////////////////////////////////////////////////////////////////// Callback function used with the SHBrowseForFolder function. INT CALLBACK CFolderDialog::BrowseCallbackProc( IN HWND   hWnd, IN UINT   uMsg, IN LPARAM lParam, IN LPARAM lpData ){CFolderDialog* pThis = (CFolderDialog*)lpData;ASSERT( pThis != NULL );INT nRet = 0;pThis->m_hWnd = hWnd;switch( uMsg ){case BFFM_INITIALIZED:pThis->OnInitialized();break;case BFFM_SELCHANGED:pThis->OnSelChanged( (LPITEMIDLIST)lParam );break;case BFFM_VALIDATEFAILED:nRet = pThis->OnValidateFailed( (LPCTSTR)lParam );break;case BFFM_IUNKNOWN:pThis->OnIUnknown( (IUnknown*)lParam );break;default:ASSERT( FALSE );break;}pThis->m_hWnd = NULL;return nRet;}/////////////////////////////////////////////////////////////////////////////// Commands, valid to call only from handlersVOID CFolderDialog::SetExpanded( IN LPCTSTR pszPath ){USES_CONVERSION;ASSERT( m_hWnd != NULL );ASSERT( AfxIsValidString( pszPath, MAX_PATH ) );::SendMessage( m_hWnd, BFFM_SETEXPANDED, (WPARAM)TRUE, (LPARAM)T2CW( pszPath ));}VOID CFolderDialog::SetOKText( IN LPCTSTR pszText ){USES_CONVERSION;ASSERT( m_hWnd != NULL );::SendMessage( m_hWnd, BFFM_SETOKTEXT, (WPARAM)0, (LPARAM)T2CW( pszText ));}VOID CFolderDialog::EnableOK( IN BOOL bEnable /*TRUE*/ ){ASSERT( m_hWnd != NULL ); ::SendMessage( m_hWnd, BFFM_ENABLEOK, (WPARAM)bEnable, 0L);}VOID CFolderDialog::SetSelection( IN LPITEMIDLIST pItemIDList ){ASSERT( m_hWnd != NULL );::SendMessage( m_hWnd, BFFM_SETSELECTION, (WPARAM)FALSE, (LPARAM)pItemIDList);}VOID CFolderDialog::SetSelection( IN LPCTSTR pszPath ){ASSERT( m_hWnd != NULL );ASSERT( AfxIsValidString( pszPath, MAX_PATH ) );::SendMessage( m_hWnd, BFFM_SETSELECTION, (WPARAM)TRUE, (LPARAM)pszPath);}VOID CFolderDialog::SetStatusText( IN LPCTSTR pszText ){ASSERT( m_hWnd != NULL );::SendMessage( m_hWnd, BFFM_SETSTATUSTEXT, (WPARAM)0, (LPARAM)pszText);}// Shell version 5.0 or later:VOID CFolderDialog::SetExpanded( IN LPITEMIDLIST pItemIDList ){ASSERT( m_hWnd != NULL ); ::SendMessage( m_hWnd, BFFM_SETEXPANDED, (WPARAM)FALSE, (LPARAM)pItemIDList);}/////////////////////////////////////////////////////////////////////////////

然后调用方式:

void CDemo154Dlg::OnBnClickedButton2(){// TODO: 在此添加控件通知处理程序代码CFolderDialog dlg(NULL,NULL,NULL);if (dlg.DoModal() == IDOK){CString strPathName = dlg.GetFolderPath();DWORD dwFileAttriBures = ::GetFileAttributes(strPathName);CString strFileAttributes = _T("");if (dwFileAttriBures & FILE_ATTRIBUTE_NORMAL){strFileAttributes += _T("无\n");}if (dwFileAttriBures & FILE_ATTRIBUTE_DIRECTORY){strFileAttributes =+ _T("目录\n");}if (dwFileAttriBures & FILE_ATTRIBUTE_ARCHIVE){strFileAttributes += _T("存档\n");}if (dwFileAttriBures & FILE_ATTRIBUTE_HIDDEN){strFileAttributes += _T("隐藏\n");}if (dwFileAttriBures & FILE_ATTRIBUTE_READONLY){strFileAttributes += _T("只读\n");}if (dwFileAttriBures & FILE_ATTRIBUTE_SYSTEM){strFileAttributes += _T("系统\n");}if (dwFileAttriBures & FILE_ATTRIBUTE_TEMPORARY){strFileAttributes += _T("临时\n");}CString strText = _T("");strText.Format(_T("文件属性:\n%s"),strFileAttributes);AfxMessageBox(strText);}}void CDemo154Dlg::OnBnClickedButton3(){// TODO: 在此添加控件通知处理程序代码CFolderDialog dlg(NULL,NULL,NULL,BIF_BROWSEINCLUDEFILES);if (dlg.DoModal()){CString strPathName = dlg.GetFolderPath();DWORD dwFileAttributes = FILE_ATTRIBUTE_ARCHIVE|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY;::SetFileAttributes(strPathName,dwFileAttributes);CString strFileAttributes = _T("存档、隐藏、只读");CString strText = _T("");strText.Format(_T("文件属性:\n%s"),strFileAttributes);AfxMessageBox(strText);}}


0 0