[原]VC 实用小函数(用一个添加一个)

    BOOL CreateDirectories(LPCTSTR lpPathName,LPSECURITY_ATTRIBUTES lpSecurityAttributes)
    {
        int nPos 
= 0;
        BOOL bCreated 
= FALSE;
        CString strPath 
= lpPathName;
        strPath
.Append("\\");
        strPath
.Replace("\\\\","\\");
        CString strTemp 
= strPath.Tokenize(_T("/\\"),nPos);

        
while(strTemp != "")
        {
            CString strCurrPath(strPath
.Mid(0,nPos));
            
if(!::SetCurrentDirectory(strCurrPath))
            {
                
if(!::CreateDirectory(strCurrPath,lpSecurityAttributes))
                {
                    
return FALSE;
                }
            }

            strTemp 
= strPath.Tokenize(_T("/\\"),nPos);
        }
        
return TRUE;    
    }

    
/*
    删除除指定类型文件的指定类型的文件,可指定是否包括子目录
    
*/
    BOOL DeleteFiles(LPCTSTR lpFilePatten
,LPCTSTR lpExceptFilePatten,LPCTSTR lpFilePath,BOOL bIncludeSubDir)
    {
        INT nPos 
= 0;
        TCHAR szOldPath[MAX_PATH];
        
::GetCurrentDirectory(MAX_PATH,szOldPath);
        CFileFind ff;
        CList
<CString> FilePathList;
        CMapStringToPtr mapExceptFileName;
        FilePathList
.AddTail(lpFilePath);
        BOOL nRet 
= TRUE;
        BOOL bFinded 
= FALSE;
        BOOL bUseExcept 
= _tcslen(lpExceptFilePatten) > 0;
        
while(FilePathList.GetCount() > 0)
        {
            CString strPath(FilePathList
.RemoveHead());
            
if(!::SetCurrentDirectory(strPath))
            {
                nRet 
=  FALSE;
                
break;
            }

            
if(bUseExcept > 0)
            {
                CString strEfp(lpExceptFilePatten)
,strEach;
                INT iPos 
= 0;
                strEach 
= strEfp.Tokenize(";",iPos);
                
while(strEach != "")
                {
                    bFinded 
= ff.FindFile(strEach);
                    
while(bFinded)
                    {
                        bFinded 
= ff.FindNextFile();
                        
if(!ff.IsDirectory())
                        {
                            mapExceptFileName
.SetAt(ff.GetFileName(),NULL);
                        }

                    }

                    ff
.Close();
                    
//下一个
                    strEach = strEfp.Tokenize(";",iPos);
                }


            }

            CString strfp(lpFilePatten)
,strEach;
            INT iPos 
= 0;
            strEach 
= strfp.Tokenize(";",iPos);
            
while(strEach != "")
            {

                bFinded 
= ff.FindFile(strEach);
                
while(bFinded)
                {
                    bFinded 
= ff.FindNextFile();
                    
if(!ff.IsDirectory())
                    {
                        
if(bUseExcept)
                        {
                            LPVOID value;
                            CString strKey(ff
.GetFileName());
                            
if(mapExceptFileName.Lookup(strKey,value))
                            {
                                mapExceptFileName
.RemoveKey(strKey);
                            }
                            
else
                            {
                                
::DeleteFile(strKey);
                            }
                        }
                        
else
                        {
                            
::DeleteFile(ff.GetFileName());
                        }
                    }

                }

                ff
.Close();
                
//下一个
                strEach = strfp.Tokenize(";",iPos);
            }

            
if(bIncludeSubDir)
            {
                bFinded 
= ff.FindFile(_T("*.*"));
                
while( bFinded)
                {
                    bFinded 
= ff.FindNextFile();
                    
if(ff.IsDirectory())
                    {
                        CString strPathName 
= ff.GetFileName();
                        
if(strPathName != "." && strPathName != "..")
                        {
                            FilePathList
.AddTail(strPath +"\\" +  ff.GetFileName());
                        }
                    }
                }

                ff
.Close();
            }

        }

        
if(_tcslen(szOldPath) > 0)
        {
            
::SetCurrentDirectory(szOldPath);    
        }
    
        
return nRet;
    }

    BOOL ReadHLMRegKey(LPSTR pPath
, LPSTR pKey, LPVOID pValue /*最长512Byte*/)
    {
        HKEY    hKey;
        DWORD    dwValType
, 
                dwBuffLen 
= 512;

        
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, pPath, 0, KEY_READ, &hKey) == ERROR_SUCCESS) 
        {
            
if(RegQueryValueEx(hKey, pKey, NULL, &dwValType, (LPBYTE) pValue, &dwBuffLen) == ERROR_SUCCESS)
            {
                RegCloseKey(hKey);
                
return TRUE;
            }
            RegCloseKey(hKey);
        }
        
return FALSE;
    }
posted @ 2006-09-30 10:57  阿牛  阅读(371)  评论(0编辑  收藏  举报