一、文件夹的创建

 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下对文件及文件夹的操作(复制、剪切、删除、创建文件夹,写文件)的更多相关文章

  1. Linux基础------文件打包解包---tar命令,文件压缩解压---命令gzip,vim编辑器创建和编辑正文件,磁盘分区/格式化,软/硬链接

    作业一:1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cat /etc/passwd /etc/group > /1.txt2) 将用户信息数据库文件和用户 ...

  2. linux批量复制或删除同命名规则的文件

    如图所示,有多个不同后缀的文件,但他们的前缀都是"QC_TZ.impute." 此时想批量复制图中的文件的话,可以考虑用命令行 cp QC_TZ.impute.* /your/de ...

  3. Java基础之写文件——创建通道并且写文件(TryChannel)

    控制台程序,创建一个文件并且使用通道将一些文本写入到这个文件中. import static java.nio.file.StandardOpenOption.*; import java.nio.c ...

  4. C# 对文件与文件夹的操作包括删除、移动与复制

    在.Net中,对文件(File)和文件夹(Folder)的操作可以使用File类和Directory类,也可以使用FileInfo类和DirectoryInfo类.文件夹(Folder)是只在Wind ...

  5. 无法删除 NTFS 盘上的文件或文件夹(对Windows文件的各种情况有比较详细的描述)

    简介 本文介绍您可能无法删除 NTFS 文件系统卷上的文件或文件夹的原因,以及如何分析造成此问题的不同原因从而解决此问题. 更多信息 注意:在内部,NTFS 将文件夹作为特殊类型的文件进行处理.因此, ...

  6. DriverStore文件夹特别大,能删除吗?

    DriverStore文件夹特别大,能删除吗? DriverStore\FileRepository文件夹特别大,能删除吗? C:\Windows\System32\DriverStore\FileR ...

  7. [转载]DriverStore文件夹特别大,能删除吗?

    [转载]DriverStore文件夹特别大,能删除吗? 转自博客园https://www.cnblogs.com/lovebing/p/6951833.html 这篇文章,清理完C盘多了20G!不要太 ...

  8. MFC下MCI的使用播放音乐

    最近研究了一下MFC下的音乐的播放,主要使用了MCI 1.需要包含的库文件 在链接资源里(link)添加库文件VFW32.lib winmm.lib 2.包含的头文件 #include <mms ...

  9. C++ 文件的复制、删除、重命名

    一.文件的复制 #include <iostream>#include <fstream>using namespace std; int CopyFile(char *Sou ...

随机推荐

  1. 注册 Gmail,验证手机号码的时候提示“此号码不能用于验证”,怎么解决?

    地址:https://www.zhihu.com/question/34834773   刚刚注册成功.折腾了好久,最后换了Chrome浏览器就成功了.手机号在注册的第一个界面时就填+86 xxxxx ...

  2. tomcat + jenkins启动tomcat后打开jenkins页面提示404错误的解决方案

    首先下载tomcat和jenkins,将下载的jenkins2.12 war放到tomcat的webapps文件夹下后执行/bin文件夹下的start启动后,打开http://localhost:80 ...

  3. 图片的 base64 编码

    图片的 base64 编码就是将一幅图片编码成一串字符串,使用该字符串代替图像地址.我们所看到的网页上的图片,都是需要消耗一个 http 请求下载而来的:(所有才有了 csssprites 技术< ...

  4. hdu5514 非2的次幂容斥原理

    /* 1 126 223092870 210 330 390 462 510 546 570 690 714 770 798 858 910 966 1122 1155 1190 1254 1326 ...

  5. Url有值怎么使用get传值

    原来url有数据 test 我们可以$_GET可以获取出来 一般form表单头用get方式都可以满足大多需求 但有一种情况 url里已经有值的时候 用url就会覆盖原来的值 而数据就会丢失 : 数据又 ...

  6. Android:关于Edittext的一些设置

    1.自动弹出输入框. et_order_search.setFocusableInTouchMode(true); et_order_search.requestFocus(); CmzBossApp ...

  7. [Rails] 设计模式 MVC

    使用 MVC 的Web开发框架有很多,比如 PHP 的 Zend,Python 的 Django 和 Golang 的 Beego.这篇文章主要介绍一下 MVC 的基本概念. 何为MVC 为了明确一个 ...

  8. C# WebBrowser禁止F5刷新

    在用写一个桌面软件的过程中,用到webbrowser实现界面.这时有一个禁止webbrowser通过f5按键进行刷新的要求.本着边做边学的原则,本菜查了一下百度,原来这么简单,代码如下: this.w ...

  9. iScroll屏幕滑动函数封装总结

    //iScroll.js屏幕滚动函数 function funScroll(a,b) { var d; function beforload() { d = new iScroll(a, { chec ...

  10. LSM树由来、设计思想以及应用到HBase的索引(转)

    转自: http://www.cnblogs.com/yanghuahui/p/3483754.html 讲LSM树之前,需要提下三种基本的存储引擎,这样才能清楚LSM树的由来: 哈希存储引擎  是哈 ...