我在一个项目中工作,我需要知道一个文件在目录中是否是唯一的。那么,我如何才能发现一个文件是否存在于目录中呢?我有不带扩展名的文件名和目录的路径。
发布于 2011-12-16 00:18:20
我想这里没有现成的函数,但是你可以使用下面这样的函数:
static bool fileExists( const char *path )
{
const DWORD attr = ::GetFileAttributesA( path );
return attr != INVALID_FILE_ATTRIBUTES &&
( ( attr & FILE_ATTRIBUTE_ARCHIVE ) || ( attr & FILE_ATTRIBUTE_NORMAL ) );
}这验证了它是一个“正常”文件。如果您还想处理隐藏文件,则可能需要添加/删除标志检查。
发布于 2011-12-16 00:27:49
我更喜欢在C++上做这件事,但是你提到了一个Visual-C++标签,所以有一种方法可以在Visual-C++.NET上做:
using <mscorlib.dll>
using namespace System;
using namespace System::IO;
bool search(String folderPath, String fileName) {
String* files[] = Directory::GetFiles(folderPath, fileName+".*"); //search the file with the name fileName with any extension (remember, * is a wildcard)
if(files->getLength() > 0)
return true; //there are one or more files with this name in this folder
else
return false; //there arent any file with this name in this folder
}https://stackoverflow.com/questions/8523132
复制相似问题