一、C++文件夹重命名方法:
使用MoveFile()方法即可,源文件夹名字和目标文件夹名字都需要完整的路径名称
if (g_oCamera[0].strCodedData != _T(""))
{
CString strNgDir = _T("D:/image/") + strDay + _T("/") + strTestTime;
/*QDir dirNgImg(strNgDir);
if (dirNgImg.exists())
dirNgImg.rename(strTestTime, MultiThrDlg->strCodedData);*/
if(PathIsDirectory(strNgDir))
{
//将strTestTime名字重命名成g_oCamera[0].strCodedData
CString dst = strNgDir; //得到文件夹所在目录
int len = strNgDir.GetLength(); //得到路径长度
int index = strNgDir.ReverseFind('/') + 1;
dst.Delete(index, len - index);
dst += g_oCamera[0].strCodedData;
if(PathIsDirectory(dst)) //如果已经存在目标文件夹,删掉
pThis->DeleteDirectory(dst);
if (MoveFile(strNgDir, dst))//重命名文件夹
//pThis->MessageBox(_T("重命名完成"));
;
else
//pThis->MessageBox(_T("重命名失败"));
;
}
}
else
g_oCamera[0].strCodedData = strTestTime;
/删除文件夹以及文件夹里面的所有内容
bool CMhSystemDlg::DeleteDirectory(CString DirName)
{
CString pubPath;
pubPath=DirName;
CFileFind finder;
DirName+=_T("\\*.*");
BOOL isFinded=(BOOL)finder.FindFile(DirName);
while(isFinded)
{
isFinded=(BOOL)finder.FindNextFileW();
if(!finder.IsDots())
{
CString strDirName;
strDirName+=pubPath;
strDirName+=_T("\\");
strDirName+=finder.GetFileName();
if(finder.IsDirectory())
DeleteDirectory(strDirName); /递归调用
else
{
SetFileAttributes(strDirName,FILE_ATTRIBUTE_NORMAL); //去掉文件的系统和隐藏属性
DeleteFile(strDirName);
}
}
}
finder.Close();
if(!RemoveDirectory(pubPath))
return false;
return true;
}
二、Qt文件夹重命名方法:
使用QDir dirO(strPath); dirO.rename()方法
if (strDataCode != strTestTimeee)
{
/文件夹重命名
QString strPath = QString::fromLocal8Bit("D:/image/") + strDayyy + QString::fromLocal8Bit("/PointImage/") + strTestTimeee;
QString strPath1 = QString::fromLocal8Bit("D:/image/") + strDayyy + QString::fromLocal8Bit("/PointImage/") + strDataCode;
QDir dirO(strPath);
if (dirO.exists())
dirO.rename(strPath, strPath1);
}
三、C#文件夹重命名方法:
使用System.IO.Directory.Move()方法
if (strDataCode != strTestTimeee)
{
/文件夹重命名
String strPath = "D:/image/" + strDayyy + "/PointImage/" + strTestTimeee;
String strPath1 = "D:/image/" + strDayyy + "/PointImage/" + strDataCode;
if (System.IO.Directory.Exists(strPath))
{
if (System.IO.Directory.Exists(strPath1))
System.IO.Directory.Delete(strPath1, true);
System.IO.Directory.Move(strNgDir, strPath1);
}
}
四、C#复制文件、文件夹
c#没有复制目录的代码,需要通过递归实现复制目录:
使用方法:
1、把d:\temp\index目录下的所有子目录和文件复制到 d:\temp\newindex目录下。
bool copy = CopyDirectory("d:\\temp\\index\\", "d:\\temp\\newindex\\", true);
2、具体代码实现
需要引用System.IO命名空间,实现代码如下:
private static bool CopyDirectory(string SourcePath, string DestinationPath, bool OverWriteExisting)
{
bool ret = false;
try
{
SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";
if (Directory.Exists(SourcePath))
{
if (Directory.Exists(DestinationPath) == false)
Directory.CreateDirectory(DestinationPath);
foreach (string fls in Directory.GetFiles(SourcePath))
{
FileInfo flinfo = new FileInfo(fls);
flinfo.CopyTo(DestinationPath + flinfo.Name, OverWriteExisting);
}
foreach (string drs in Directory.GetDirectories(SourcePath))
{
DirectoryInfo drinfo = new DirectoryInfo(drs);
if (CopyDirectory(drs, DestinationPath + drinfo.Name, OverWriteExisting) == false)
ret = false;
}
}
ret = true;
}
catch (Exception ex)
{
ret = false;
}
return ret;
}
3、直接把代码拷贝到程序里面就大功告成