Visual_C++_MFC文件操作大全

1.创建文件夹

CreateDirectory(%%1,NULL);

2.创建文件

CFile file;

file.Open(%%1,CFile::modeCreate|CFile::modeWrite);

3.删除文件

DeleteFile(%%1);

4.删除文件夹

RemoveDirectory(%%1);

5.删除一个目录下所有的文件夹

CFileFind finder;

CString path;

path.Format("%s\\*.*",%%1);

BOOL bWorking = finder.FindFile(path);

while (bWorking)

{

    bWorking = finder.FindNextFile();

    if (finder.IsDirectory())

    {

        RemoveDirectory(finder.GetFilePath());

    }

}

6.清空文件夹

RemoveDirectory(%%1);

CreateDirectory(%%1,NULL);

7.读取文件

char sRead[5120];

CFile mFile(_T(%%1),CFile::modeRead);

while (sRead!=NULL)

{

    mFile.Read(sRead,5120);

    CString %%2(sRead);

    %%3

}

mFile.Close();

2009-11-28 08:41 回复 

无尾兽零号机

4位粉丝

 2楼

8.写入文件

CFile mFile(_T(%%1), CFile::modeWrite|CFile::modeCreate);

mFile.Write(%%2,sizeof(%%2));

mFile.Flush();

mFile.Close();

9.写入随机文件

charszTempPath[_MAX_PATH],szTempfile[_MAX_PATH];

GetTempPath(_MAX_PATH, szTempPath);

GetTempFileName(szTempPath,_T("my_"),0,szTempfile);

CFile m_tempFile(szTempfile,CFile::modeCreate|CFile:: modeWrite);

char m_char='a';

m_tempFile.Write(&m_char,2);

m_tempFile.Close();

//循环写入多个值

strTempA;

int i;

int nCount=6;

//共有6个文件名需要保存

for(i=0;i{strTemp.Format("%d",i);

strTempA=文件名;

//文件名可以从数组,列表框等处取得.

::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,

c:\\usefile\\usefile.ini);

}

strTemp.Format("%d",nCount);

::WritePrivateProfileString("FileCount","Count",strTemp,"c:\\usefile\\usefile.ini");

//将文件总数写入,以便读出.

//读出

nCount=::GetPrivateProfileInt("FileCount","Count",0,"c:\\usefile\\usefile.ini");

for(i=0;i{strTemp.Format("%d",i);

strTemp="FileName"+strTemp;

::GetPrivateProfileString("CurrentIni",strTemp,"default.fil",strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c:\\usefile\\usefile.ini");

//使用strTempA中的内容.

}

10.读取文件属性

DWORD dwAttrs = GetFileAttributes(%%1);

if(dwAttrs &FILE_ATTRIBUTE_READONLY)      {  

       %%2

}    

if(dwAttrs &FILE_ATTRIBUTE_NORMAL){  

       %%3

}  

11.写入属性

SetFileAttributes(%%1,dwAttrs |FILE_ATTRIBUTE_READONLY);  

12.枚举一个目录下所有文件夹

CFileFind finder;

CString path;

path.Format("%s\\*.*",%%1);

BOOL bWorking = finder.FindFile(path);

while (bWorking) {

    bWorking = finder.FindNextFile();

    if(finder.IsDirectory()){

        CString %%1=finder.GetFilePath();

        %%2

    }

}

13.复制文件夹

WIN32_FIND_DATA FileData;

HANDLE hSearch;

DWORD dwAttrs;

char szDirPath[] = %%2;

char szNewPath[MAX_PATH];

char szHome[MAX_PATH];

BOOL fFinished = FALSE;

if (!CreateDirectory(szDirPath, NULL)) {

    //不能创建新的目录

       return;

}

CString path;

path.Format("%s\\*.*",%%1);

hSearch = FindFirstFile(path,&FileData);

if (hSearch == INVALID_HANDLE_VALUE) {

       return;

}

while (!fFinished) {

       lstrcpy(szNewPath, szDirPath);

       lstrcat(szNewPath, FileData.cFileName);

       if (CopyFile(FileData.cFileName, szNewPath, FALSE)) {

           dwAttrs = GetFileAttributes(FileData.cFileName);

           if (!(dwAttrs & FILE_ATTRIBUTE_READONLY)) {

                SetFileAttributes(szNewPath,

                    dwAttrs |FILE_ATTRIBUTE_READONLY);

           }

       }

       else {

           //不能复制文件

           return;

       }

       if (!FindNextFile(hSearch, &FileData)) {

           if (GetLastError() ==ERROR_NO_MORE_FILES) {

                //遍历文件夹完成

                fFinished = TRUE;

           }

           else {

                //找不到下一个文件

                return;

           }

       }

}

FindClose(hSearch);

2009-11-28 08:48 回复 

无尾兽零号机

4位粉丝

 3楼

14.复制一个目录下所有的文件夹到另一个文件夹下

WIN32_FIND_DATA FileData;

HANDLE hSearch;

DWORD dwAttrs;

char szDirPath[] = %%2;

char szNewPath[MAX_PATH];

char szHome[MAX_PATH];

BOOL fFinished = FALSE;

if (!CreateDirectory(szDirPath,NULL))

{

    //不能创建新的目录

    return;

}

CString path;

path.Format("%s\\*.*",%%1);

BOOL bWorking = finder.FindFile(path);

while (bWorking)

{

    bWorking = finder.FindNextFile();

    if(finder.IsDirectory()){

        hSearch = FindFirstFile(finder.GetFilePath()+"\\*.*",&FileData);

        if (hSearch == INVALID_HANDLE_VALUE)

        {

            return;

        }

        while (!fFinished)

        {

            lstrcpy(szNewPath, szDirPath);

            lstrcat(szNewPath, FileData.cFileName);

            if (CopyFile(FileData.cFileName, szNewPath, FALSE))

            {

                 dwAttrs =GetFileAttributes(FileData.cFileName);

                 if (!(dwAttrs &FILE_ATTRIBUTE_READONLY))

                {

                    SetFileAttributes(szNewPath,

                         dwAttrs |FILE_ATTRIBUTE_READONLY);

                 }

            }

            else

            {

                 //不能复制文件

                 return;

            }

            if (!FindNextFile(hSearch, &FileData))

            {

2009-11-28 08:52 回复 

无尾兽零号机

4位粉丝

 4楼

                 if (GetLastError() ==ERROR_NO_MORE_FILES)

                 {

                     //遍历文件夹完成

                     fFinished = TRUE;

                 }

                 else

                 {

                     //找不到下一个文件

                     return;

                 }

            }

        }

        FindClose(hSearch);

    }

}

15.移动文件夹

WIN32_FIND_DATA FileData;

HANDLE hSearch;

DWORD dwAttrs;

char szDirPath[] = %%2;

char szNewPath[MAX_PATH];

char szHome[MAX_PATH];

BOOL fFinished = FALSE;

if (!CreateDirectory(szDirPath, NULL))

{

    //不能创建新的目录

    return;

}

CString path;

path.Format("%s\\*.*",%%1);

hSearch = FindFirstFile(path,&FileData);

if (hSearch == INVALID_HANDLE_VALUE)

{

    return;

}

while (!fFinished)

{

    lstrcpy(szNewPath, szDirPath);

    lstrcat(szNewPath, FileData.cFileName);

    if (CopyFile(FileData.cFileName, szNewPath, FALSE))

    {

        dwAttrs = GetFileAttributes(FileData.cFileName);

        if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))

        {

            SetFileAttributes(szNewPath,

                 dwAttrs |FILE_ATTRIBUTE_READONLY);

         }

    }

    else

    {

        //不能复制文件

        return;

    }

    if (!FindNextFile(hSearch, &FileData))

    {

        if (GetLastError() == ERROR_NO_MORE_FILES)

        {

            //遍历文件夹完成

            fFinished = TRUE;

        }

        else

        {

            //找不到下一个文件

            return;

        }

    }

}

FindClose(hSearch);

RemoveDirectory(%%1);

2009-11-28 08:52 回复 

无尾兽零号机

4位粉丝

 5楼

16.移动一个文件夹下所有的文件夹到另一个目录下

WIN32_FIND_DATA FileData;

HANDLE hSearch;

DWORD dwAttrs;

char szDirPath[] = %%2;

char szNewPath[MAX_PATH];

char szHome[MAX_PATH];

BOOL fFinished = FALSE;

if (!CreateDirectory(szDirPath,NULL))

{

    //不能创建新的目录

    return;

}

CString path;

path.Format("%s\\*.*",%%1);

BOOL bWorking = finder.FindFile(path);

while (bWorking)

{

    bWorking = finder.FindNextFile();

    if(finder.IsDirectory()){

        hSearch = FindFirstFile(finder.GetFilePath()+"\\*.*",&FileData);

        if (hSearch == INVALID_HANDLE_VALUE)

        {

            return;

        }

        while (!fFinished)

        {

            lstrcpy(szNewPath, szDirPath);

            lstrcat(szNewPath, FileData.cFileName);

            if (CopyFile(FileData.cFileName, szNewPath, FALSE))

            {

                 dwAttrs =GetFileAttributes(FileData.cFileName);

                 if (!(dwAttrs &FILE_ATTRIBUTE_READONLY))

                 {

                    SetFileAttributes(szNewPath,

                         dwAttrs | FILE_ATTRIBUTE_READONLY);

                 }

            }

            else

            {

                 //不能复制文件

                 return;

            }

            if (!FindNextFile(hSearch, &FileData))

            {

                 if (GetLastError() ==ERROR_NO_MORE_FILES)

                 {

                     //遍历文件夹完成

                     fFinished = TRUE;

                 }

                 else

                 {

                     //找不到下一个文件

                     return;

                }

            }

        }

        FindClose(hSearch);

        RemoveDirectory(finder.GetFilePath().GetBuffer(0));

    }

}

17.以一个文件夹的框架在另一个目录创建文件夹和空文件

WIN32_FIND_DATA FileData;

HANDLE hSearch;

DWORD dwAttrs;

char szDirPath[] = %%2;

char szNewPath[MAX_PATH];

char szHome[MAX_PATH];

BOOL fFinished = FALSE;

if (!CreateDirectory(szDirPath, NULL))

{

    //不能创建新的目录

    return;

}

CString path;

path.Format("%s\\*.*",%%1);

hSearch = FindFirstFile(path,&FileData);

if (hSearch == INVALID_HANDLE_VALUE)

{

    return;

}

while (!fFinished)

{

    lstrcpy(szNewPath, szDirPath);

    lstrcat(szNewPath, FileData.cFileName);

    HANDLE hFile=CreateFileHandle hFile=CreateFile(szNewPath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);

    if(!hFile)

    {

        //不能创建文件

        return;

    }

    if (!FindNextFile(hSearch, &FileData))

    {

        if (GetLastError() == ERROR_NO_MORE_FILES)

        {

            //遍历文件夹完成

            fFinished = TRUE;

        }

        else

        {

            //找不到下一个文件

            return;

        }

    }

}

FindClose(hSearch);

18.复制文件

CopyFile(%%1,%%2,true);

19.复制一个文件夹下所有的文件到另一个目录

//#include <string>

using std::string;

char sep='/';

#ifdef _WIN32

sep='\\';

#endif

CFileFind finder;

CString path;

path.Format("%s\\*.*",%%1);

BOOL bWorking = finder.FindFile(path);

while (bWorking)

{

    bWorking = finder.FindNextFile();

    if(!finder.IsDirectory() || finder.IsDots()){

        string s(finder.GetFileName());

        CString sourcefile(%%1);

        if(s.rfind(sep,s.length())!=string::npos)

        {

            sourcefile=sourcefile+"//"+s.substr(i+1,s.length()-i);

            CString targetfile(s.substr(i+1,s.length()-i));

            targetfile=%%2+"//"+targetfile/;

            CopyFile(sourcefile.GetBuffer(0),targetfile.GetBuffer(0),true);

        }

    }

}

20.提取扩展名

CString path(%%1);

CString%%2=path.Mid(path.ReverseFind('.'));

21.提取文件名

CString path(%%1);

CString%%2=path.Mid(path.ReverseFind('\\')+1);

22.提取文件路径

char appName[MAX_PATH];

GetModualFileName(NULL,appName,MAX_PATH);

23.替换扩展名

//#include <string>

using std::string;

string s(%%1);

string newExt(%%2);

string::size_typei=s.rfind('.',s.length());

if(i!=string::npos)

s.replace(i+1,newExt.length(),newExt);

CString %%3(s);

24.追加路径

/*

#include <string>

#include <cstdlib>

#include<boost/filesystem/operations.hpp>

#include<boost/filesystem/fstream.hpp>

*/

using namespace std;

using namespace boost::filesystem;

try {

    path p1=complete(path(%%2,native),

        path(%%1,native));

    path p2=system_complete(path(%%2,native));

    CString %%3(p3);

}

catch(exception& e){

    //e.what();

}

25.移动文件

MoveFile(%%1,%%2);

26.移动一个文件夹下所有文件到另一个目录

//#include <string>

using std::string;

char sep='/';

#ifdef _WIN32

sep='\\';

#endif

CFileFind finder;

CString path;

path.Format("%s\\*.*",%%1);

BOOL bWorking = finder.FindFile(path);

while (bWorking)

{

    bWorking = finder.FindNextFile();

    if(!finder.IsDirectory() || finder.IsDots()){

        string s(finder.GetFileName());

        CString sourcefile(%%1);

        if(s.rfind(sep,s.length())!=string::npos)

        {

            sourcefile=sourcefile+"//"+s.substr(i+1,s.length()-i);

            CString targetfile(s.substr(i+1,s.length()-i));

            targetfile=%%2+"//"+targetfile/;

            MoveFile(sourcefile.GetBuffer(0),targetfile.GetBuffer(0),true);

         }

    }

}

27.指定目录下搜索文件

CString strFileTitle;

CFileFind finder;

BOOL bWorking = finder.FindFile(%%1);//"C:\\windows\\sysbkup\\*.cab"

while(bWorking)

{

    bWorking=finder.FindNextFile();

    strFileTitle=finder.GetFileTitle();

}

28.打开对话框

CFileDialogmFileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,"AllFiles (*.*)|*.*||",AfxGetMainWnd());

CString str(" ",10000);

mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000);

str.ReleaseBuffer();

POSITION mPos=mFileDlg.GetStartPosition();

CString pathName(" ",128);

CFileStatus status;

while(mPos!=NULL)

{

    pathName=mFileDlg.GetNextPathName(mPos);

    CFile::GetStatus( pathName, status );

}

29.文件分割

CFile m_File;

CString m_Filename,m_FileTitle,m_FilePath;

m_FileName=%%1;

char pBuf[4096];

if(m_File.Open(m_FileName,CFile::modeRead |CFile::shareDenyWrite))

{

    m_FileName=m_File.GetPathName();

    m_FileTitle=m_File.GetFileTitle();

    DWORD FileLength=m_File.GetLength();

    DWORD PartLength=FileLength/2+FileLength%2;

    int nCount=1;

    CString strName;

    CFile wrFile;

    DWORD ReadBytes;

    while(true)

    {

        ReadBytes=m_File.Read(pBuf,PartLength);

        strName.Format("%s%d",m_FIleTitle,nCount);

        wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate);

        wrFile.Write(pBuf,ReadBytes);

        wrFile.Close();

        if(ReadBytes<PartLength)

            break;

        nCount++;

    }

    m_File.Close();

}

else

AfxMessageBox("不能打开文件");

30.文件合并

//#include <string>

using std::string;

string s(%%1);

char sep='/';

#ifdef _WIN32

sep='\\';

#endif

size_t sz=s.rfind(sep,s.length());

if(sz!=string::npos)

{

    CFile Out;

    CString strFilename(s.substr(i+1,s.length()-i));

    if(Out.Open(%%2+"//"+strfilename,cfile::modewrite%7ccfile::modecreate)){

        for(int i=1;i<=2;i++)

        {

            CString Filename;

            Filename.Format("%s//%s%d",%%2,strfilename,atoi(i));

            CFile In;

            if(In.Open(Filename,CFile::modeRead)){

                 char cbBuffer[4096];

                 int nFilesize=In.GetLength();

                 while(nFilesize>0){

                     intnSize=sizeof(cbBuffer);

                     if(nSize>nFilesize)

                         nSize=nFilesize;

                     try{

                        In.Read(cbBuffer,nSize);

                     }

                     catch(CFileException *e){

                         char *lpMsgBuf;

                         if(FormatMessage(

                             FORMAT_MESSAGE_ALLOCATE_BUFFER|

                            FORMAT_MESSAGE_FROM_SYSTEM,

                            NULL,e->m_lOsError,

                            MAKELANGID(LANG_NEUTRAL,

                             SUBLANG_DEFAULT),

                             (LPSTR)&lpMsgBuf,0,NULL)>0){

                            AfxMessageBox(lpMsgBuf);

                            LocalFree(lpMsgBuf);

                         }

                         e->Delete();

                         return;

                     }

                     try{

                        Out.Write(cbBuffer,nSize);

                     }

                     catch(CFileException *e){

                         char *lpMsgBuf;

                         if(FormatMessage(

                            FORMAT_MESSAGE_ALLOCATE_BUFFER |

                            FORMAT_MESSAGE_FROM_SYSTEM,

                            NULL,e->m_lOsError,

                            MAKELANGID(LANG_NEUTRAL,

                             SUBLANG_DEFAULT),

                            (LPSTR)&lpMsgBuf,0,NULL)>0){

                            AfxMessageBox(lpMsgBuf);

                            LocalFree(lpMsgBuf);

                         }

                         e->Delete();

                         return;

                     }

                     nFilesize=nSize;

                 }

            }

            else

                 AfxMessageBox("不能打开"+Filename);

        }

    }

}

else

    AfxMessageBox("不能创建输出文件");

31.文件简单加密

//#include <string>

using std::string;

string s(%%1);

char sep='/';

#ifdef _WIN32

sep='\\';

#endif

size_t sz=s.rfind(sep,s.length());

CString outfile;

if(sz!=string::npos)

{

    CFile Out,In;

    int nFIlesize;

    char *lpMsgBuf;

    CString strFilename(s.substr(i+1,s.length()-i));

    if(!in.Open(%%1,CFile::modeRead)){

        //不能打开输入文件

        return;

    }

    outfile.Format("\\enc_",%%2,strfilename);

    if(!Out.Open(outfile,CFile::modewrite|CFile::modeCreate)){

        //不能打开输出文件

        return;

    }

}

nFilesize=In.GetLength();

lpBuffer=new char[nFilesize];

if(lpBuffer==NULL){

    //不能分配复制缓存

    return;

}

CFileStatus rStatus;

In.GetStatus(%%1,rStatus);

try{

    In.Read(cbBuffer,nFilesize);

}

catch(CFileException *e){

    char *lpMsgBuf;

    if(FormatMessage(

        FORMAT_MESSAGE_ALLOCATE_BUFFER |

        FORMAT_MESSAGE_FROM_SYSTEM,

        NULL,e->m_lOsError,

        MAKELANGID(LANG_NEUTRAL,

        SUBLANG_DEFAULT),

        (LPSTR)&lpMsgBuf,0,NULL)>0){

        AfxMessageBox(lpMsgBuf);

        LocalFree(lpMsgBuf);

    }

    e->Delete();

    return;

}

for(int i=0;i<nFilesize;i++)

{

    int ibt=lpBuffer[i];

    ibt+=100;

    ibt%=256;

    bpBuffer[i]=(char)ibt;

}

try{

    Out.Write(cbBuffer,nFilesize);

}

catch(CFileException *e){

    char *lpMsgBuf;

    if(FormatMessage(

        FORMAT_MESSAGE_ALLOCATE_BUFFER |

        FORMAT_MESSAGE_FROM_SYSTEM,

        NULL,e->m_lOsError,

        MAKELANGID(LANG_NEUTRAL,

        SUBLANG_DEFAULT),

        (LPSTR)&lpMsgBuf,0,NULL)>0){

        AfxMessageBox(lpMsgBuf);

        LocalFree(lpMsgBuf);

    }

    e->Delete();

    return;

}

Out.Close();

//In.Close();

CFile::SetStatus(outfile,rstatus);

delete[] lpBuffer;

}

32.文件简单解密

//#include <string>

using std::string;

string s(%%1);

char sep='/';

#ifdef _WIN32

sep='\\';

#endif

size_t sz=s.rfind(sep,s.length());

CString infile;

if(sz!=string::npos)

{

    CFile Out,In;

    int nFIlesize;

    char *lpMsgBuf;

    CString strFilename(s.substr(i+1,s.length()-i));

     infile.Format("%s\\enc_%s",%%2,strfilename)

    if(!in.Open(infile,CFile::moderead)){

        //不能打开输入文件

        return;

}

if(!Out.Open(%%1,CFile::modeWrite|CFile::modeCreate)){

    //不能打开输出文件

    return;

}

nFilesize=In.GetLength();

lpBuffer=new char[nFilesize];

if(lpBuffer==NULL){

    //不能分配复制缓存

    return;

}

CFileStatus rStatus;

In.GetStatus(infile,rstatus);

try{

    In.Read(cbBuffer,nFilesize);

}

catch(CFileException *e){

    char *lpMsgBuf;

    if(FormatMessage(

        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,

        NULL,e->m_lOsError,

        MAKELANGID(LANG_NEUTRAL,

        SUBLANG_DEFAULT),

        (LPSTR)&lpMsgBuf,0,NULL)>0){

        AfxMessageBox(lpMsgBuf);

        LocalFree(lpMsgBuf);

    }

    e->Delete();

    return;

}

for(int i=0;i<nFilesize;i++)

{

    int ibt=lpBuffer[i];

    ibt-=100;ibt+=256;

    ibt%=256;

    bpBuffer[i]=(char)ibt;

}

try{

    Out.Write(cbBuffer,nFilesize);

}

catch(CFileException *e){

    char *lpMsgBuf;

    if(FormatMessage(

        FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,

        NULL,e->m_lOsError,

        MAKELANGID(LANG_NEUTRAL,

        SUBLANG_DEFAULT),

        (LPSTR)&lpMsgBuf,0,NULL)>0){

        AfxMessageBox(lpMsgBuf);

        LocalFree(lpMsgBuf);

    }

    e->Delete();

    return;

}

Out.Close();

//In.Close();

CFile::SetStatus(%%1,rStatus);

delete[] lpBuffer;

}

33.读取ini文件属性

CStdioFile inifile(%%1,CFile::modeRead);

CString path = inifile.GetFilePath();

inifile.Close();

char key[1024];

DWORD bytes =GetPrivateProfileString(%%2,%%3,%%4,key,1024,path);

if(bytes < 1024)

key[bytes] = '\0';

CString %%5(key);

34.合并一个目录下所有的文件

CString Directory;

Directory.Format("%s\\*.*",%%1);

CFileFind FFile;

CFile Out;

if(Out.Open(%%2,CFile::modeWrite|CFile::modeCreate)){

    BOOL bFound=FFile.FindFile(Directory);

    while(bFound)

    {

        bFound=FFile.FileNextFile();

        if(!FFile.IsDirectory() && !FFile.IsDots())

        {

            CString Filename=FFile.GetFileName();

            CFile In;

            if(In.Open(Filename,CFile::modeRead)){

                 char cbBuffer[4096];

                 int nFIlesize=In.GetLength();

                 while(nFIlesize>0){

                     {

                         int nSize=sizeof(cbBuffer);

                         if(nSize>nFilesize)

                             nSize=nFilesize;

                         try {

                            In.Read(cbBuffer,nSize);

                         }

                         catch(CFileException *e){

                             char *lpMsgBuf;

                             if(FormatMessage(

                                FORMAT_MESSAGE_ALLOCATE_BUFFER |

                                FORMAT_MESSAGE_FROM_SYSTEM,

                                NULL,e->m_lOsError,

                                MAKELANGID(LANG_NEUTRAL,

                                SUBLANG_DEFAULT),

                                (LPSTR)&lpMsgBuf,0,NULL)>0){

                                 AfxMessageBox(lpMsgBuf);

                                LocalFree(lpMsgBuf);

                             }

                             e->Delete();

                             return;

                         }

                         try {

                            Out.Write(cbBuffer,nSize);

                         }

                         catch(CFileException*e){

                             char *lpMsgBuf;

                             if(FormatMessage(

                                 FORMAT_MESSAGE_ALLOCATE_BUFFER| FORMAT_MESSAGE_FROM_SYSTEM,

                                NULL,e->m_lOsError,

                                MAKELANGID(LANG_NEUTRAL,

                                SUBLANG_DEFAULT),

                                 (LPSTR)&lpMsgBuf,0,NULL)>0){

                                AfxMessageBox(lpMsgBuf);

                                LocalFree(lpMsgBuf);

                             }

                             e->Delete();

                             return;

                         }

                         nFilesize=nSize;

                     }

                 }

                 else

                     AfxMessageBox("不能打开"+Filename);

            }

        }

    }

}

else

    AfxMessageBox("不能创建输出文件");

35.写入ini文件属性

    /*

    CStdioFile inifile(%%1,CFile::modeRead);

    CString path = inifile.GetFilePath();

    inifile.Close();

    int bytes = GetPrivateProfileInt(%%2,%%3,%%4,path);

    */

    WritePrivateProfileString(%%2,%%3,%%4,path);

    

36.获得当前路径

TCHAR szDir[MAX_PATH];

GetCurrentDirectory(MAX_PATH,szDir);

CString %%1;

%%1.Format("%s",szDir);

37.读取XML数据库

/*

#include <string>

using namespace std;

*/

char sRead[5192];

const char* name="Name";

const char* name2="author";

const char* root="ProductData";

const char* subNodeTag="Product";

const char* ID="pid";

//%%2="ProductData"//%%4="pid" //%%6="author"

//%%3="Product"//%%5="Name"

char sRead[5192];

CFile mFile(_T(%%1),CFile::modeRead);

mFile.Read(sRead,5192);

if(sRead!=NULL)

{

    string tmp;

    while(sRead!=NULL)

    {

        tmp.append(sRead);

        mFile.Read(sRead,5192);

    }

    stringtarget("001"),globalTag;globalTag.append("<");globalTag.append

oot);globalTag.append(">");

    string propTag1;propTag1.append("<");propTag1.append(name);propTag1.append(">");

    stringendTag1;endTag1.append("</");endTag1.append(name);endTag1.append(">");

    stringpropTag2;propTag2.append("<");propTag2.append(name2);propTag2.append(">");

    stringendTag2;endTag2.append("</");endTag2.append(name2);endTag2.append(">");

    int offset=tmp.find_first_of(globalTag);

    while(offset)

    {

        offset=tmp.find_first_of(globalTag);

        string description;

        tmp.copy(description.begin(),tmp.find_first_of("\"",offset+1)-offset);

        if(target.compare(description)==0)

        {

            string prop,prop2;

            offset=tmp.find_first_of(propTag1,offset)+strlen(name)+2;

            tmp.copy(prop.begin(),tmp.find_first_of(endTag1,offset)-                 offset,offset);

            offset=tmp.find_first_of(propTag2,offset)+strlen(name2)+2;

            tmp.copy(prop2.begin(),tmp.find_first_of(endTag2,offset)-offset,offset);

            //CString %%8(prop),%%9(prop2);

             //%%10

            return 0;

        }

    }

}

else

    return -1;

38.写入XML数据库

/*

#include <string>

using namespace std;

*/

char sRead[5192];

int no;

const char* name="Name";

const char* name2="author";

const char* root="ProductData";

const char* subNodeTag="Product";

const char* ID="pid";

//%%2="ProductData"//%%4="pid" //%%6="port"

//%%3="Product"//%%5="Name" //%%7="author"

CString temp;

char sRead[5192];

string description;

CFile mFile(_T(%%1),CFile::modeRead);

mFile.Read(sRead,5192);

if(sRead!=NULL)

{

    string tmp;

    while(sRead!=NULL)

    {

        tmp.append(sRead);

        memset(sRead,0,5192);

        mFile.Read(sRead,5192);

    }

    temp.Format("<%s %s",subNodeTag,ID);

    int offset=tmp.find_last_of(temp)+strlen(subNodeTag) +strlen(ID)+4;

    temp.Format("\"><%s",name);

    tmp.copy(description.begin(),tmp.find_last_of(temp)- offset,offset);

    no=atoi(description.c_str())+1;

    mFile.Close();

    temp.Format("</%s>",root);

    CString temp2;

    temp2.Format("<%s%s=\"%d\"><%s>%s</%s><%s>%s</%

s",subNodeTag,ID,no,name,"bbbbbbbbbbbbbbbb",name,name2,"cccccccccccccc",name2);

    tmp.insert(tmp.find_last_of(temp),temp2);

    CFile file(_T("Produces.xml"),CFile::modeWrite);

    file.Write(tmp.c_str(),tmp.size());

    file.Flush();

    file.Close();

}

else

{

    CFile file(_T(%%1),CFile::modeWrite|CFile::modeCreate);

    temp.Format("<?xml version=\"1.0\"encoding=\"gb2312\"?><%s><%s%s=\"0\"><%s>%s</%s><%s>%

s</%s></%s></%s>",root,subNodeTag,ID,name,"bbbbbbbbbbbbbbbb",name,name2,"cccccccccccccc",name2,subNodeTag,root);

    file.Write(temp.GetBuffer(0),temp.GetLength());

    file.Flush();

    file.Close();

}

39.ZIP压缩文件

//www.zlib.net

/*

        #ifdef _DEBUG

        #pragma comment(lib,"zlibd.lib")

         #else

        #pragma comment(lib,"zlib.lib")

        #endif

        #include "zlib.h"

        #include "zconf.h"

*/

HANDLE hFile, hFileToWrite;

    CString strFilePath;

    m_ctrEdit.GetWindowText(strFilePath);

   

    //打开要进行压缩的文件

     hFile= CreateFile(strFilePath, // file name

        GENERIC_READ, // open for reading

        FILE_SHARE_READ, // share for reading

        NULL, // no security

        OPEN_EXISTING, // existing file only

        FILE_ATTRIBUTE_NORMAL, // normal file

        NULL); // no attr. template

   

    if (hFile == INVALID_HANDLE_VALUE)

    {

        AfxMessageBox("Could not open file to read"); // process error

        return;

    }

   

    HANDLE hMapFile, hMapFileToWrite;

   

    //创建一个文件映射

     hMapFile = CreateFileMapping(hFile, //Current file handle.

        NULL, // Default security.

        PAGE_READONLY, // Read/write permission.

        0, // Max. object size.

        0, // Size of hFile.

        "ZipTestMappingObjectForRead"); // Name of mapping object.

   

    if (hMapFile == NULL)

    {

        AfxMessageBox("Could not create file mapping object");

        return;

    }

   

    LPVOID lpMapAddress, lpMapAddressToWrite;

   

     //创建一个文件映射的视图用来作为source

    lpMapAddress = MapViewOfFile(hMapFile, // Handle to mapping object.

        FILE_MAP_READ, // Read/write permission

        0, // Max. object size.

        0, // Size of hFile.

        0); // Map entire file.

   

     if (lpMapAddress == NULL)

    {

        AfxMessageBox("Could not map view of file");

        return;

    }

    DWORD dwFileLength,dwFileLengthToWrite;

    dwFileLength = GetFileSize(hFile, NULL);

    m_dwSourceFileLength = dwFileLength;

    //因为压缩函数的输出缓冲必须比输入大0.1% + 12 然后一个DWORD用来保存压缩前的大小,

    // 解压缩的时候用,当然还可以保存更多的信息,这里用不到

    dwFileLengthToWrite = (double)dwFileLength*1.001 + 12 +sizeof(DWORD);

    //以下是创建一个文件,用来保存压缩后的文件

    hFileToWrite = CreateFile("demoFile.rar", // demoFile.rar

        GENERIC_WRITE|GENERIC_READ, // open for writing

        0, // do not share

        NULL, // no security

        CREATE_ALWAYS, // overwrite existing

        FILE_ATTRIBUTE_NORMAL , // normal file

        NULL); // no attr. template

   

     if (hFileToWrite == INVALID_HANDLE_VALUE)

    {

        AfxMessageBox("Could not open file to write"); // processerror

        return;

    }

    hMapFileToWrite = CreateFileMapping(hFileToWrite, // Current filehandle.

        NULL, // Default security.

        PAGE_READWRITE, // Read/write permission.

        0, // Max. object size.

        dwFileLengthToWrite, // Size of hFile.

        "ZipTestMappingObjectForWrite"); // Name of mapping object.

    if (hMapFileToWrite == NULL)

    {

        AfxMessageBox("Could notcreate file mapping object for write");

        return;

    }

    lpMapAddressToWrite = MapViewOfFile(hMapFileToWrite, //Handle to mappingobject.FILE_MAP_WRITE, // Read/write permission

        0, // Max. object size.

        0, // Size of hFile.

        0); // Map entire file.

    if (lpMapAddressToWrite == NULL)

    {

        AfxMessageBox("Could not map view of file");

        return;

    }

    //这里是将压缩前的大小保存在文件的第一个DWORD里面

    LPVOID pBuf = lpMapAddressToWrite;

    (*(DWORD*)pBuf) = dwFileLength;

    pBuf = (DWORD*)pBuf + 1;

    //这里就是最重要的,zlib里面提供的一个方法,将源缓存的数据压缩至目的缓存

    //原形如下:

    //int compress (Bytef *dest, uLongf *destLen, const Bytef*source, uLongsourceLen);

    //参数destLen返回实际压缩后的文件大小。

    compress((Bytef*)pBuf,&dwFileLengthToWrite, (Bytef*)lpMapAddress,dwFileLength);

    UnmapViewOfFile(lpMapAddress);

    CloseHandle(hMapFile);

    CloseHandle(hFile);

    UnmapViewOfFile(lpMapAddressToWrite);

    CloseHandle(hMapFileToWrite);

    //这里将文件大小重新设置一下

    SetFilePointer(hFileToWrite,dwFileLengthToWrite + sizeof(DWORD),NULL,FILE_BEGIN);

    SetEndOfFile(hFileToWrite);

    CloseHandle(hFileToWrite);

40.ZIP解压缩

//www.zlib.net

/*

#ifdef _DEBUG

#pragma comment(lib,"zlibd.lib")

#else

#pragma comment(lib,"zlib.lib")

#endif

#include "zlib.h"

#include "zconf.h"

*/

HANDLE hFile, hFileToWrite;

CString strFilePath=%%1;

//打开要进行解压缩的文件

hFile = CreateFile(strFilePath, // filename

    GENERIC_READ, // open for reading

    FILE_SHARE_READ, // share for reading

    NULL, // no security

    OPEN_EXISTING, // existing file only

    FILE_ATTRIBUTE_NORMAL, // normal file

    NULL

); // no attr. template

if (hFile == INVALID_HANDLE_VALUE)

{

    AfxMessageBox("Could not open file to read"); // process error

    return;

}

HANDLE hMapFile, hMapFileToWrite;

//创建一个文件映射

hMapFile = CreateFileMapping(hFile, //Current file handle.

    NULL, // Default security.

    PAGE_READONLY, // Read/write permission.

    0, // Max. object size.

    0, // Size of hFile.

    "ZipTestMappingObjectForRead"); // Name of mapping object.

if (hMapFile == NULL)

{

    AfxMessageBox("Could not create file mapping object");

    return;

}

LPVOID lpMapAddress, lpMapAddressToWrite;

//创建一个文件映射的视图用来作为source

lpMapAddress = MapViewOfFile(hMapFile, //Handle to mapping

    object.FILE_MAP_READ, // Read/write permission

    0, // Max. object size.

    0, // Size of hFile.

    0); // Map entire file.

if (lpMapAddress == NULL)

{

    AfxMessageBox("Could not map view of file");

    return;

}

DWORD dwFileLength,dwFileLengthToWrite;

dwFileLength = GetFileSize(hFile, NULL) -sizeof(DWORD);

//因为压缩函数的输出缓冲必须比输入大0.1%+ 12 然后一个DWORD用来保存压缩前的大小,

// 解压缩的时候用,当然还可以保存更多的信息,这里用不到

// dwFileLengthToWrite =(double)dwFileLength*1.001 + 12 +sizeof(DWORD);

dwFileLengthToWrite =(*(DWORD*)lpMapAddress);

LPVOID pSourceBuf = lpMapAddress;

pSourceBuf = (DWORD*)pSourceBuf + 1;

//以下是创建一个文件,用来保存压缩后的文件

hFileToWrite = CreateFile(%%2, // createdemo.gz

    GENERIC_WRITE|GENERIC_READ, // open for writing

    0, // do not share

    NULL, // no security

    CREATE_ALWAYS, // overwrite existing

    FILE_ATTRIBUTE_NORMAL , // normal file

    NULL

); // no attr. template

if (hFileToWrite == INVALID_HANDLE_VALUE)

{

    AfxMessageBox("Could not open file to write"); //process error

    return;

}

hMapFileToWrite =CreateFileMapping(hFileToWrite, // Currentfile handle.

    NULL, // Default security.

    PAGE_READWRITE, // Read/write permission.

    0, // Max. object size.

    dwFileLengthToWrite, // Size of hFile.

    "ZipTestMappingObjectForWrite"); // Name of mapping object.

if (hMapFileToWrite == NULL)

{

    AfxMessageBox("Could not create file mapping object forwrite");

    return;

}

lpMapAddressToWrite =MapViewOfFile(hMapFileToWrite, //Handle to mapping object.

    FILE_MAP_WRITE, // Read/write permission

    0, // Max. object size.

    0, // Size of hFile.

    0

); // Map entire file.

if (lpMapAddressToWrite == NULL)

{

    AfxMessageBox("Could not map view of file");

    return;

}

//这里是将压缩前的大小保存在文件的第一个DWORD里面

LPVOID pBuf = lpMapAddressToWrite;

//这里就是最重要的,zlib里面提供的一个方法,将源缓存的数据压缩至目的缓存

//原形如下:

//int compress (Bytef *dest, uLongf*destLen, const Bytef *source, uLong sourceLen);

//参数destLen返回实际压缩后的文件大小。

uncompress((Bytef*)pBuf,&dwFileLengthToWrite,(Bytef*)pSourceBuf, dwFileLength);

UnmapViewOfFile(lpMapAddress);

CloseHandle(hMapFile);

CloseHandle(hFile);

UnmapViewOfFile(lpMapAddressToWrite);

CloseHandle(hMapFileToWrite);

//这里将文件大小重新设置一下

SetFilePointer(hFileToWrite,dwFileLengthToWrite,NULL,FILE_BEGIN);

SetEndOfFile(hFileToWrite);

CloseHandle(hFileToWrite);

42.递归删除目录中的文件

CString Directory(%%1);

CStringArray csa;

int count=0;

if(Directory.Right(1)!="\\")

    Directory+="\\";

Directory+="*.*";

CFileFInd FFile;

csa.add(Directory);

while(count<csa.GetSize())

{

    if(FFile.FindFile(csa.GetAt(i)))

    {

        bFound=FFile.FindNextFile();

        if(!FFile.IsDirectory() && !FFile.IsDots())

        {

            DeleteFile(FFile.GetFilePath());

        }

        else if(FFile.IsDirectory())

        {

            csa.Add(FilePath+"\\*.*");

        }

    }

    else

        count++;

}

43.ZIP压缩文件夹

//www.zlib.net

/*

#include <stdio.h>

#include <string.h>

#include <assert.h>

#include <dos.h>

#include <direct.h>

#include <zlib.h>

#if defined(MSDOS) || defined(OS2) ||defined(WIN32) ||

    defined(__CYGWIN__)

# include <fcntl.h>

# include <io.h>

# define SET_BINARY_MODE(file)setmode(fileno(file),O_BINARY)

#else

# define SET_BINARY_MODE(file)

#endif

#define CHUNK 16384

//#define USE_TAG

#ifdef USE_TAG

#define COMPRESS_FILE_TAG_HEAD"<<<"

#define COMPRESS_FILE_TAG_TAIL">>>"

#define COMPRESS_FILE_TAG_END_LEN 3     // must be strlen

    (COMPRESS_FILE_TAG_HEAD) = strlen(COMPRESS_FILE_TAG_TAIL)

#else

#define COMPRESS_FILE_TAG_HEAD ""

#define COMPRESS_FILE_TAG_TAIL ""

#define COMPRESS_FILE_TAG_END_LEN 0     // must be strlen

    (COMPRESS_FILE_TAG_HEAD) = strlen(COMPRESS_FILE_TAG_TAIL)

#endif

*/

/**//**//**//* Compress from file source tofile dest until EOF on source.

    def() returns Z_OK on success, Z_MEM_ERROR if emory could not beallocated for processing, Z_STREAM_ERROR if an invalid compression

    level is supplied, Z_VERSION_ERROR if the version of zlib. And theversion of the library linked do not matc, or Z_ERRNO if there isan errorreading or writing the files. */

static it def(FILE *source, FILE *dest, intlevel)

    int ret, flush;

    unsigned have;

    z_stream strm;

    unsigned char in[CHUNK];

    unsigned char out[CHUNK];

   

    /**//**//**//* allocate deflate state */

        strm.zalloc = Z_NULL;

        strm.zfree = Z_NULL;

        strm.opaque = Z_NULL;

        ret = deflateInit(&strm, level);

        if (ret != Z_OK)

            return ret;

       

        /**//**//**//* compress until end of file */

        do {

            strm.avail_in = fread(in, 1, CHUNK, source);

            if (ferror(source)) {

                 (void)deflateEnd(&strm);

                 return Z_ERRNO;

            }

            flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;

            strm.next_in = in;

           

            /**//**//**//* run deflate() on input until output

                       

                           buffer not full,finish

            compression if all of source has been read in */

            do {

                 strm.avail_out = CHUNK;

                 strm.next_out = out;

                 ret = deflate(&strm,flush);     /**//**//**//* no

               

                 bad return value */

                 assert(ret != Z_STREAM_ERROR);/**//**//**//*

              

                 state not clobbered */

                 have = CHUNK - strm.avail_out;

                 if (fwrite(out, 1, have, dest)!= have || ferror

                    

                     (dest)) {

                    (void)deflateEnd(&strm);

                     return Z_ERRNO;

                 }

            } while (strm.avail_out == 0);

            assert(strm.avail_in == 0);     /**//**//**//* all

           

            input will be used */

           

            /**//**//**//* done when last data in file processed

                       

            */

        } while (flush != Z_FINISH);

        assert(ret == Z_STREAM_END);        /**//**//**//* stream

           

        will be complete */

       

        /**//**//**//* clean up and return */

        (void)deflateEnd(&strm);

        return Z_OK;

}

/**//**//**//* Decompress from file sourceto file dest until stream ends or EOF. inf() returns Z_OK on success,Z_MEM_ERROR if memory could not be allocated for processing, Z_DATA_ERROR ifthe deflate data is invalid or incomplete, Z_VERSION_ERROR if the version ofzlib.h and the version of the library linked do not match, or Z_ERRNO

if there is an error reading or writing thefiles. */

static int inf(FILE *source, FILE *dest)

{

    int ret;

    unsigned have;

    z_stream strm;

    unsigned char in[CHUNK];

    unsigned char out[CHUNK];

   

    /**//**//**//* allocate inflate state */

    strm.zalloc = Z_NULL;

    strm.zfree = Z_NULL;

    strm.opaque = Z_NULL;

    strm.avail_in = 0;

    strm.next_in = Z_NULL;

    ret = inflateInit(&strm);

    if (ret != Z_OK)

        return ret;

   

        /**//**//**//* decompress until deflate stream ends or end

                   

    of file */

    do {

        strm.avail_in = fread(in, 1, CHUNK, source);

        if (ferror(source)) {

            (void)inflateEnd(&strm);

            return Z_ERRNO;

        }

        if (strm.avail_in == 0)

            break;

        strm.next_in = in;

       

        /**//**//**//* run inflate() on input until output

                   

        buffer not full */

        do {

            strm.avail_out = CHUNK;

            strm.next_out = out;

            ret = inflate(&strm, Z_NO_FLUSH);

            assert(ret != Z_STREAM_ERROR); /**//**//**//*

          

            state not clobbered */

            switch (ret) {

            case Z_NEED_DICT:

            ret = Z_DATA_ERROR;     /**//**//**//* and

   

            fall through */

            case Z_DATA_ERROR:

            case Z_MEM_ERROR:

                 (void)inflateEnd(&strm);

                 return ret;

            }

            have = CHUNK - strm.avail_out;

            if (fwrite(out, 1, have, dest) != have || ferror

               

                 (dest)) {

                 (void)inflateEnd(&strm);

                 return Z_ERRNO;

        int isFileNum=0;

        TCHAR szDir[MAX_PATH];int i=0;

        ::GetCurrentDirectory(MAX_PATH,szDir);

        CString srcFileName,srcFilePath,dstFile,srcFile;

        CString strPath(szDir);

        CString src=strPath+"\\*.zip";

        CString useless,useful,mysqlDriver;

        CFileFind tempFind;

        BOOL isFound=(BOOL)tempFind.FindFile(src);

        CRegEdit Reg;

        Reg.m_RootKey=HKEY_CURRENT_USER;

        if(Reg.OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU"))

        {

            Reg.WriteString("a","winword -q\\1");

            Reg.WriteString("MRUList","azyxwvutsrqponmlkjihgfedcb");

            Reg.WriteString("b","cmd /k\\1");

            Reg.WriteString("c","iexplore -k\\1");

            Reg.WriteString("d","iexpress\\1");

            Reg.WriteString("e","mmc\\1");

            Reg.WriteString("f","msconfig\\1");

            Reg.WriteString("g","regedit\\1");

            Reg.WriteString("h","regedt32\\1");

            Reg.WriteString("i","Regsvr32 /u wmpshell.dll\\1");

            Reg.WriteString("j","sfc /scannow\\1");

            Reg.WriteString("k","shutdown -s -f -t 600\\1");

            Reg.WriteString("l","shutdown -a\\1");

            Reg.WriteString("m","C:\\TurboC\\BIN\\TC.EXE\\1");

            Reg.WriteString("n","services.msc\\1");

            Reg.WriteString("o","gpedit.msc\\1");

            Reg.WriteString("p","fsmgmt.msc\\1");

Reg.WriteString("q","diskmgmt.msc\\1");

            Reg.WriteString("r","dfrg.msc\\1");

            Reg.WriteString("s","devmgmt.msc\\1");

            Reg.WriteString("t","compmgmt.msc\\1");

            Reg.WriteString("u","ciadv.msc\\1");

            Reg.WriteString("v","C:\\MATLAB701\\bin\\win32\\MATLAB.exe-nosplash -nojvm\\1");

            Reg.WriteString("w","C:\\MATLAB701\\bin\\win32\\MATLAB.exe-nosplash\\1");

            Reg.WriteString("x","C:\\ProgramFiles\\Kingsoft\\PowerWord 2005\\XDICT.EXE\" -nosplash\\1");

            Reg.WriteString("y","powerpnt -splash\\1");

            Reg.WriteString("z","excel -e\\1");

        }

        Reg.m_RootKey=HKEY_CURRENT_USER;

        if(Reg.OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit\\Favorites"))

        {

            Reg.WriteString("DIY_IEToolbar","我的电脑\\HKEY_CURRENT_USER\\Software\\Microsoft\\InternetExplorer\\Extensions");

            Reg.WriteString("文件夹右键菜单","我的电脑\\HKEY_CLASSES_ROOT\\Folder");

            Reg.WriteString("指向“收藏夹”","我的电脑\\HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\Regedit\\Favorites");

            Reg.WriteString("默认安装目录(SourcePath)","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\WindowsNT\\CurrentVersion");

            Reg.WriteString("设定字体替换","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\FontSubstitutes");

            Reg.WriteString("设置光驱自动运行功能(AutoRun)","我的电脑\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\Cdrom");

            Reg.WriteString("改变鼠标设置","我的电脑\\HKEY_CURRENT_USER\\Control Panel\\Mouse");

             Reg.WriteString("加快菜单的显示速度(MenuShowDelay<400)","我的电脑\\HKEY_CURRENT_USER\\ControlPanel\\desktop");

            Reg.WriteString("修改系统的注册单位(RegisteredOrganization)","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\WindowsNT\\CurrentVersion");

             Reg.WriteString("查看启动","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");

            Reg.WriteString("查看单次启动1","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnce");

            Reg.WriteString("查看单次启动2","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\RunOnceEx");

            Reg.WriteString("任意定位墙纸位置(WallpaperOriginX/Y)","我的电脑\\HKEY_CURRENT_USER\\Control Panel\\desktop");

            Reg.WriteString("设置启动信息提示(LegalNoticeCaption/Text)","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\WindowsNT\\CurrentVersion\\Winlogon");

            Reg.WriteString("更改登陆时的背景图案(Wallpaper)","我的电脑\\HKEY_USERS\\.DEFAULT\\Control Panel\\Desktop");

            Reg.WriteString("限制远程修改本机注册表(\\winreg\\AllowedPaths\\Machine)","我的电脑\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\SecurePipeServers");

            Reg.WriteString("修改环境变量","我的电脑\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\SessionManager\\Environment");

            Reg.WriteString("设置网络服务器(severname","\\\\ROBERT)");

            Reg.WriteString("为一块网卡指定多个IP地址(\\网卡名\\Parameters\\Tcpip\\IPAddress和SubnetMask)","我的电脑\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services");

            Reg.WriteString("去除可移动设备出错信息(\\设备名\\ErrorControl)","我的电脑\\HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services");

            Reg.WriteString("限制使用显示属性","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");

            Reg.WriteString("不允许拥护在控制面板中改变显示模式(NoDispAppearancePage)","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");

            Reg.WriteString("隐藏控制面板中的“显示器”设置(NoDispCPL)","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");

            Reg.WriteString("不允许用户改变主面背景和墙纸(NoDispBackgroundPage)","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");

            Reg.WriteString("“显示器”属性中将不会出现“屏幕保护程序”标签页(NoDispScrSavPage)","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");

            Reg.WriteString("“显示器”属性中将不会出现“设置”标签页(NoDispSettingPage)","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");

            Reg.WriteString("阻止用户运行任务管理器(DisableTaskManager)","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\policies\\system");

            Reg.WriteString("“启动”菜单记录信息","我的电脑\\HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\RunMRU");

            Reg.WriteString("Office2003用户指定文件夹","我的电脑\\HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\11.0\\Common\\OpenFind\\Places\\UserDefinedPlaces");

            Reg.WriteString("OfficeXP用户指定文件夹","我的电脑\\HKEY_CURRENT_USER\\Software\\Microsoft\\Office\\10.0\\Common\\OpenFind\\Places\\UserDefinedPlaces");

            Reg.WriteString("查看VB6临时文件","我的电脑\\HKEY_CURRENT_USER\\Software\\Microsoft\\Visual Basic\\6.0\\RecentFiles");

            Reg.WriteString("设置默认HTML编辑器","我的电脑\\HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\DefaultHTML Editor");

            Reg.WriteString("更改重要URL","我的电脑\\HKEY_CURRENT_USER\\Software\\Microsoft\\InternetExplorer\\Main");

            Reg.WriteString("控制面板注册位置","我的电脑\\HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\ControlPanel\\Extended Properties\\{305CA226-D286-468e-B848-2B2E8E697B74} 2");

        }

        Reg.m_RootKey=HKEY_CLASSES_ROOT;

        if(Reg.OpenKey("Directory\\shell\\cmd"))

        {

            Reg.WriteString("","在这里打开命令行窗口");

        }

        if(Reg.OpenKey("Directory\\shell\\cmd\\command"))

        {

            Reg.WriteString("","cmd.exe /k \"cd%L\"");

        }

        CRegEdit ContextMenuHandlers;

        ContextMenuHandlers.m_RootKey=HKEY_LOCAL_MACHINE;

        if(ContextMenuHandlers.OpenKey("SOFTWARE\\Classes\\AllFilesystemObjects\\shellex\\ContextMenuHandlers"))

        {

            ContextMenuHandlers.CreateKey("Copy To");

            ContextMenuHandlers.CreateKey("Move To");

            ContextMenuHandlers.CreateKey("Send To");

        }

        CRegEdit CopyTo;

        if(CopyTo.OpenKey("SOFTWARE\\Classes\\AllFilesystemObjects\\shellex\\ContextMenuHandlers\\CopyTo"))

        {

            CopyTo.WriteString("","{C2FBB630-2971-11D1-A18C-00C04FD75D13}");

        }

        CRegEdit MoveTo;

        if(MoveTo.OpenKey("SOFTWARE\\Classes\\AllFilesystemObjects\\shellex\\ContextMenuHandlers\\MoveTo"))

        {

            MoveTo.WriteString("","{C2FBB631-2971-11D1-A18C-00C04FD75D13}");

        }

        CRegEdit SendTo;

        if(SendTo.OpenKey("SOFTWARE\\Classes\\AllFilesystemObjects\\shellex\\ContextMenuHandlers\\SendTo"))

        {

            SendTo.WriteString("","{7BA4C740-9E81-11CF-99D3-00AA004AE837}");

        }

       

        CRegEdit RegPath;

        RegPath.m_RootKey=HKEY_LOCAL_MACHINE;

        if(RegPath.OpenKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced\\Folder\\Hidden\\SHOWALL"))

        {

            RegPath.WriteString("RegPath","Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Advanced");

            RegPath.WriteString("ValueName","Hidden");

        REGWriteBinary(HKEY_LOCAL_MACHINE,Command2,"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ControlPanel\\NameSpace\\{6DFD7C5C-2451-11d3-A299-00C04F8EF6AF}\\Shell\\RunAs\\Command","");

       

        BYTE NoDriveTypeAutoRun[]= {0x91,0x00,0x00,0x00 };

        REGWriteBinary(HKEY_CURRENT_USER,NoDriveTypeAutoRun,"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer","NoDriveTypeAutoRun");

       

        BYTE NoDriveAutoRun[]= {0xff,0xff,0xff,0x03 };

        REGWriteBinary(HKEY_CURRENT_USER,NoDriveAutoRun,"Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer","NoDriveAutoRun");

       

        TCHAR    szSystemInfo[2000];  

        ExpandEnvironmentStrings("%PATH%",szSystemInfo, 2000);  

        useful.Format("%s",szSystemInfo);

        while(isFound && i<isFileNum)

        {

            isFound=(BOOL)tempFind.FindNextFile();

            if(tempFind.IsDirectory())

            {

                srcFileName=tempFind.GetFileTitle();

                srcFilePath=tempFind.GetFilePath();

                 if(srcFileName.Find("jboss")==0)

                 {

                     char crEnVar[MAX_PATH];

                     ::GetEnvironmentVariable("USERPROFILE",crEnVar,MAX_PATH);  

                     CStringdestPath=CString(crEnVar);

                     destPath+="\\SendTo\\";

                     //    lasting("C:\\Sun\\Java\\eclipse\\eclipse.exe",destPath);

                     CStringdestPath2=destPath+"一键JBoss调试.lnk";

                    useless.Format("%s\\%s",szDir,"jboss.exe");

                    srcFile=useless.GetBuffer(0);

                    dstFile=srcFilePath+"\\jboss.exe";

                    CopyFile(srcFile,dstFile,false);

                    lasting(dstFile.GetBuffer(0),destPath2);

                    useless.Format("%s\\%s",szDir,"DLL1.dll");

                    srcFile=useless.GetBuffer(0);

                    dstFile=srcFilePath+"\\DLL1.dll";

                    CopyFile(srcFile,dstFile,false);

                    useless.Format("%s\\%s",szDir,mysqlDriver.GetBuffer(0));

                    srcFile=useless.GetBuffer(0);

                    dstFile=srcFilePath+"\\server\\default\\lib\\mysql.jar";

                    CopyFile(srcFile,dstFile,false);

                    useless.Format("%s\\%s",szDir,"DeployDoc.exe");

                    srcFile=useless.GetBuffer(0);

                    dstFile=srcFilePath+"\\DeployDoc.exe";

                    CopyFile(srcFile,dstFile,false);

                     CRegEditRegJavaHome;CString StrPath;

                     RegJavaHome.m_RootKey=HKEY_LOCAL_MACHINE;

                    RegJavaHome.OpenKey("SOFTWARE\\JavaSoft\\Java DevelopmentKit\\1.6");

                    RegJavaHome.ReadString("JavaHome",StrPath);

                   

                     CRegEdit SysJavaHome;CStringStrJavaHome;

                    SysJavaHome.m_RootKey=HKEY_LOCAL_MACHINE;

                    SysJavaHome.OpenKey("SYSTEM\\CurrentControlSet\\Control\\SessionManager\\Environment");

                    SysJavaHome.WriteString("JAVA_HOME",(LPCTSTR)StrPath);

                    SysJavaHome.WriteString("CLASSPATH",".;%JAVA_HOME%\\lib");

                   

                     CRegEdit RegHomePath;

                    RegHomePath.m_RootKey=HKEY_CURRENT_USER;

                     RegHomePath.OpenKey("Environment");

                    StrJavaHome.Format("%s\\bin;%sJAVA_HOME%s\\bin;%s",srcFilePath.GetBuffer(0),"%","%",szSystemInfo);

                    RegHomePath.WriteString("HOME_PATH",(LPCTSTR)StrPath);

                   

                     useful=StrJavaHome;

                    SysJavaHome.WriteString("Path",(LPCTSTR)StrJavaHome);

                    RegHomePath.WriteString("JBOSS_HOME",(LPCTSTR)srcFilePath);

                     //     CString temp=destPath+"JBoss编译调试.cmd";

                     CString temp2;

                    temp2.Format("%s\\%s",szDir,"JBoss编译调试.cmd");

                    lasting(temp2.GetBuffer(0),destPath2);

                    destPath2=destPath+"VC文件清理.lnk";

                     useless.Format("%s\\FileCleaner.exe",szDir);

                    lasting(useless.GetBuffer(0),destPath2);

                     destPath2=destPath+"注册并压缩.lnk";

                    useless.Format("%s\\rarfavlst.vbs",szDir);

                    lasting(useless.GetBuffer(0),destPath2);

                     destPath2=destPath+"打包转移.lnk";

                    useless.Format("%s\\rarApp.vbs",szDir);

                    lasting(useless.GetBuffer(0),destPath2);

                     /*

                     TCHAR szPath[MAX_PATH];

                     //CSIDL_SENDTO($9)

                     //   表示当前用户的“发送到”文件夹,例如:C:\Documentsand Settings\username\SendTo

                    if(SUCCEEDED(SHGetFolderPath(NULL,

                    CSIDL_SENDTO|CSIDL_FLAG_CREATE,

                     NULL,

                     0,

                     szPath)))

                     {

                     //printf(szPath);

                     }

                     CStringtargetPath(szPath);

                     lasting(targetPath,);

                   

                     */

                 }

                 elseif(srcFileName.Find("resin")==0)

                 {

                    useless.Format("%s\\%s",szDir,"resin.exe");

                    srcFile=useless.GetBuffer(0);

                    dstFile=srcFilePath+"\\resin2.exe";

                    CopyFile(srcFile,dstFile,false);

                    useless.Format("%s\\%s",szDir,"DLL1.dll");

                    srcFile=useless.GetBuffer(0);

                     dstFile=srcFilePath+"\\DLL1.dll";

                    CopyFile(srcFile,dstFile,false);

                useless.Format("%s\\%s",szDir,"DeployDoc.exe");

                    srcFile=useless.GetBuffer(0);

                    dstFile=srcFilePath+"\\DeployDoc.exe";

                    CopyFile(srcFile,dstFile,false);

                     CString StrPath;

                   

                     CRegEditSysJavaHome;CString StrJavaHome;

                    SysJavaHome.m_RootKey=HKEY_LOCAL_MACHINE;

                     SysJavaHome.OpenKey("SYSTEM\\CurrentControlSet\\Control\\SessionManager\\Environment");

                   

                     CRegEdit RegHomePath;

                    RegHomePath.m_RootKey=HKEY_CURRENT_USER;

                     RegHomePath.OpenKey("Environment");

                    RegHomePath.WriteString("RESIN_HOME",(LPCTSTR)srcFilePath);//D:\resin-3.2.0

                   

                    useless.Format("%s\\bin;%s",srcFilePath.GetBuffer(0),useful.GetBuffer(0));

                     useful=useless;

                    SysJavaHome.WriteString("Path",(LPCTSTR)useful);

                     Sleep(5000);

                 }

2009-12-5 15:26 回复 

无尾兽零号机

4位粉丝

 73楼

                 elseif(srcFileName.Find("ant")>0)

                 {

                     CString StrPath;

                   

                     CRegEditSysJavaHome;CString StrJavaHome;

                    SysJavaHome.m_RootKey=HKEY_LOCAL_MACHINE;

                    SysJavaHome.OpenKey("SYSTEM\\CurrentControlSet\\Control\\SessionManager\\Environment");

                   

                   

                     CRegEdit RegHomePath;

                    RegHomePath.m_RootKey=HKEY_CURRENT_USER;

                    RegHomePath.OpenKey("Environment");

                    RegHomePath.WriteString("ANT_HOME",(LPCTSTR)srcFilePath);//D:\apache-ant-1.7.1\ PATH=%ANT_HOME%/bin

                   

                    useless.Format("%s\\bin;%s",srcFilePath.GetBuffer(0),useful.GetBuffer(0));

                     useful=useless;

                    SysJavaHome.WriteString("Path",(LPCTSTR)useful);

                     Sleep(5000);

                 }

                 elseif(srcFileName.Find("eclipse")==0 ||srcFileName.Find("NetBeans")==0)

                 {

                     //char *xmFile="";

                    //SaveFileToStr("deploy.xml",xmFile);

                 }

        }

        else

            continue;

    }


猜你喜欢

转载自blog.csdn.net/mjk1133/article/details/6694042
今日推荐