MFC下对文件及文件夹的操作(复制、剪切、删除、创建文件夹,写文件)
一、文件夹的创建
void CFileOperationDlg::OnButtonMakeFolder()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFileFind m_sFileFind; if (!m_sFileFind.FindFile(m_FolderName))
{
CreateDirectory(m_FolderName,NULL);
}
}
二、文件的创建
void CFileOperationDlg::OnButtonMakeFile()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CFile m_sFile;
m_sFile.Open(m_FolderName + TEXT("\\") + m_FileName,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite|CFile::shareDenyWrite);
m_sFile.SeekToEnd();
m_sFile.Close();
}
三、文件夹的复制(包括文件的复制)
void CFileOperationDlg::OnButtonCopy()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
CString m_strDestPath;
m_strDestPath = "D:\\TestMyself\\FileOperation\\Debug";
CString m_strSrcPath = "D:\\TestMyself\\FileOperation\\VISTA";
CopyDirectory(m_strSrcPath,m_strDestPath);
} BOOL CFileOperationDlg::CopyDirectory(CString strSrcPath,CString strDestPath)
{
CFileFind m_sFileFind;
if (strSrcPath.IsEmpty())
{
OutputDebugString("源文件名为空,无法进行拷贝!");
return FALSE;
}
if (!m_sFileFind.FindFile(strDestPath))
{
CreateDirectory(strDestPath,NULL);//创建目标文件夹
}
CFileFind finder;
CString path;
path.Format("%s/*.*",strSrcPath);
//AfxMessageBox(path);
BOOL bWorking = finder.FindFile(path);
while (bWorking)
{
bWorking = finder.FindNextFile();
//AfxMessageBox(finder.GetFileName());
if (finder.IsDirectory() && !finder.IsDots())//是文件夹 而且 名称不含 . 或 ..
{
CopyDirectory(finder.GetFilePath(),strDestPath+"/"+finder.GetFileName());//递归创建文件夹+"/"+finder.GetFileName()
}
else
{//是文件,则直接复制
//AfxMessageBox("复制文件"+finder.GetFilePath());//+finder.GetFileName()
CopyFile(finder.GetFilePath(),strDestPath+"/"+finder.GetFileName(),FALSE);
}
} return TRUE;
} CString CFileOperationDlg::GetFilePath()
{
CString m_FilePath; GetModuleFileName(NULL,m_FilePath.GetBufferSetLength(MAX_PATH+),MAX_PATH);
m_FilePath.ReleaseBuffer(); int m_iPosIndex;
m_iPosIndex = m_FilePath.ReverseFind('\\');
m_FilePath = m_FilePath.Left(m_iPosIndex+); return m_FilePath;
} CString CFileOperationDlg::GetFileName()
{
CString sFileName; sFileName = CTime::GetCurrentTime().Format("%Y-%m-%d") + TEXT(".log"); return sFileName;
}
四、文件夹的删除
BOOL CFileOperationDlg::DeleteFolder(LPCTSTR lpszPath)
{
int nLength = strlen(lpszPath);
char *NewPath = new char[nLength + ];
strcpy(NewPath,lpszPath);
NewPath[nLength] = '\0';
NewPath[nLength + ] = '\0';
SHFILEOPSTRUCT FileOp;
ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
FileOp.fFlags = FOF_NOCONFIRMATION;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = NewPath;
FileOp.pTo = NULL;
FileOp.wFunc = FO_DELETE;
return SHFileOperation(&FileOp) == ;
}
五、文件夹的移动(剪切)
BOOL CFileOperationDlg::MoveFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
{
int nLengthFrm = strlen(lpszFromPath);
char *NewPathFrm = new char[nLengthFrm + ];
strcpy(NewPathFrm,lpszFromPath);
NewPathFrm[nLengthFrm] = '\0';
NewPathFrm[nLengthFrm + ] = '\0';
SHFILEOPSTRUCT FileOp;
ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
FileOp.fFlags = FOF_NOCONFIRMATION ;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = NewPathFrm;
FileOp.pTo = lpszToPath;
FileOp.wFunc = FO_MOVE; return SHFileOperation(&FileOp) == ;
}
六、文件写操作
void CFileOperationDlg::OnButtonOk()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
try
{
CFile m_sFile;
CFileFind m_FileFind;
CString m_sErrorMessage;
//CString m_sFileName = GetFileName();
//CString m_sFilePath = GetFilePath();
CString m_sCurrentTime = (CTime::GetCurrentTime()).Format("%Y-%m-%d %X"); if (!m_FileFind.FindFile(m_FolderName))
{
CreateDirectory(m_FolderName,NULL);
}
m_sFile.Open(m_FolderName + TEXT("\\") +m_FileName,CFile::modeCreate |CFile::modeNoTruncate| CFile::modeReadWrite |CFile::shareDenyWrite); m_sFile.SeekToEnd(); if (sizeof(TCHAR) == sizeof(WCHAR))
{
WORD wSignature = 0xFFFF;
m_sFile.Write(&wSignature,);
} m_sErrorMessage = TEXT("*******************") + m_sCurrentTime + TEXT("*******************")+TEXT("\r\n") ;
m_sFile.Write(m_sErrorMessage,m_sErrorMessage.GetLength()*sizeof(TCHAR)); m_RichText += TEXT("\r\n");\
m_sFile.Write(m_RichText,m_RichText.GetLength()*sizeof(TCHAR));
m_sFile.Close();
}
catch(CFileException fileException)
{
return ;
} }
以上代码测试通过!!
MFC下对文件及文件夹的操作(复制、剪切、删除、创建文件夹,写文件)的更多相关文章
- Linux基础------文件打包解包---tar命令,文件压缩解压---命令gzip,vim编辑器创建和编辑正文件,磁盘分区/格式化,软/硬链接
作业一:1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cat /etc/passwd /etc/group > /1.txt2) 将用户信息数据库文件和用户 ...
- linux批量复制或删除同命名规则的文件
如图所示,有多个不同后缀的文件,但他们的前缀都是"QC_TZ.impute." 此时想批量复制图中的文件的话,可以考虑用命令行 cp QC_TZ.impute.* /your/de ...
- Java基础之写文件——创建通道并且写文件(TryChannel)
控制台程序,创建一个文件并且使用通道将一些文本写入到这个文件中. import static java.nio.file.StandardOpenOption.*; import java.nio.c ...
- C# 对文件与文件夹的操作包括删除、移动与复制
在.Net中,对文件(File)和文件夹(Folder)的操作可以使用File类和Directory类,也可以使用FileInfo类和DirectoryInfo类.文件夹(Folder)是只在Wind ...
- 无法删除 NTFS 盘上的文件或文件夹(对Windows文件的各种情况有比较详细的描述)
简介 本文介绍您可能无法删除 NTFS 文件系统卷上的文件或文件夹的原因,以及如何分析造成此问题的不同原因从而解决此问题. 更多信息 注意:在内部,NTFS 将文件夹作为特殊类型的文件进行处理.因此, ...
- DriverStore文件夹特别大,能删除吗?
DriverStore文件夹特别大,能删除吗? DriverStore\FileRepository文件夹特别大,能删除吗? C:\Windows\System32\DriverStore\FileR ...
- [转载]DriverStore文件夹特别大,能删除吗?
[转载]DriverStore文件夹特别大,能删除吗? 转自博客园https://www.cnblogs.com/lovebing/p/6951833.html 这篇文章,清理完C盘多了20G!不要太 ...
- MFC下MCI的使用播放音乐
最近研究了一下MFC下的音乐的播放,主要使用了MCI 1.需要包含的库文件 在链接资源里(link)添加库文件VFW32.lib winmm.lib 2.包含的头文件 #include <mms ...
- C++ 文件的复制、删除、重命名
一.文件的复制 #include <iostream>#include <fstream>using namespace std; int CopyFile(char *Sou ...
随机推荐
- JqGrid的总结大全【转】
jqGrid整理 PS:JqGrid 官方 API 点我 我的笔记: 一. jqGrid的加载. 1.引用相关头文件 引入CSS: <link href="Scripts/jq ...
- Java 并发 线程的优先级
Java 并发 线程的优先级 @author ixenos 低优先级线程的执行时刻 1.在任意时刻,当有多个线程处于可运行状态时,运行系统总是挑选一个优先级最高的线程执行,只有当线程停止.退出或者由于 ...
- 【jquery、XML】jquery通过按钮使打开select
<select> <option>aaaaa</option> <option>bbbbb</option> <option>c ...
- 加速Android Studio的Gradle构建速度
在利用Android Studio做项目时,发现随着项目内资源的逐渐增多(或者项目创建时间太过久远,而又未经常打开),Android Studio的build速度也越来越慢.(P.S.在做我的CSGO ...
- vim 撤销 恢复 快捷键
u 撤销上一步的操作 Ctrl+r 恢复上一步被撤销的操作
- Linux 网络性能tuning向导
本文的目的不完全在于提供调优信息,而是在于告诉读者了解Linux kernel如何处理数据包,从而能够在 自己的实践中发挥Linux 内核协议栈最大的性能 The NIC ring buffer 接收 ...
- NIO 入门
新的输入/输出 (NIO) 库是在 JDK 1.4 中引入的.NIO 弥补了原来的 I/O 的不足,它在标准 Java 代码中提供了高速的.面向块的 I/O.通过定义包含数据的类,以及通过以块的形式处 ...
- java多线程并发编程
Executor框架 Executor框架是指java 5中引入的一系列并发库中与executor相关的一些功能类,其中包括线程池,Executor,Executors,ExecutorService ...
- Spring MVC(二)
spring mvc工作流 1A)客户端发出http请求,只要请求形式符合web.xml 文件中配置的*.action的话,就由DispatcherServlet 来处理. 1B)Dispatcher ...
- 用juery的ajax方法调用aspx.cs页面中的webmethod方法示例
juery的ajax调用aspx.cs页面中的webmethod方法:首先在 aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性,具体实现如下,感兴趣的朋友可以参考下哈,希望对大 ...