windows API有一个RemoveDirectory(path)函数,但是必须是该文件夹为空,才能删除。在这个ActiveX中,因为只有3个需要另外存放的DLL,因此我是采用一下代码新建文件夹的:
- TCHAR csStr[256];
- CString sysDirPath;
- GetSystemDirectory(csStr, 256);
- sysDirPath = (CString)csStr + (CString)_T("//playerdll");
- //在系统目录下创建playerdll文件夹,拷贝DLL,并注册
- CreateDirectory(sysDirPath,NULL);
- CString sourcefile=(CString)csStr+(CString)_T("//avformat-.dll");
- CopyFile(sourcefile,sysDirPath+"//avformat.dll",FALSE);
采用以下代码来删除文件的:
- TCHAR csStr[256];
- CString sysDirPath;
- GetSystemDirectory(csStr, 256);
- sysDirPath = (CString)csStr + (CString)_T("//playerdll");
- DeleteFile(sysDirPath+(CString)_T("//avformat.dll"));
- RemoveDirectory(sysDirPath);
因为我们知道要拷入的文件名,因此可以使用上面的方法来解决。如果我们不知道一个文件夹中有哪些文件,那只能使用递归的方法来删除了。
- bool DeleteDirectory(char* sDirName)
- {
- CFileFind tempFind;
- char sTempFileFind[200] ;
- sprintf(sTempFileFind,"%s/*.*",sDirName);
- BOOL IsFinded = tempFind.FindFile(sTempFileFind);
- while (IsFinded)
- {
- IsFinded = tempFind.FindNextFile();
- if (!tempFind.IsDots())
- {
- char sFoundFileName[200];
- strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(200));
- if (tempFind.IsDirectory())
- {
- char sTempDir[200];
- sprintf(sTempDir,"%s/%s",sDirName,sFoundFileName);
- DeleteDirectory(sTempDir);
- }
- else
- {
- char sTempFileName[200];
- sprintf(sTempFileName,"%s/%s",sDirName,sFoundFileName);
- DeleteFile(sTempFileName);
- }
- }
- }
- tempFind.Close();
- if(!RemoveDirectory(sDirName))
- {
- return FALSE;
- }
- return TRUE;
- }
- BOOL DelAll()
- {
- CString m_strDir="D://test";
- if(PathFileExists(m_strDir))
- DeleteDirectory((LPSTR)(LPCTSTR)m_strDir);
- return 1;
- }
注:sprintf用来实现连接字符串和格式化字符串。