class CFileSystem {
public:
CFileSystem(CFileSystem&) = delete;
CFileSystem(CFileSystem&&) = delete;
CFileSystem(){}
/// <summary>
/// 判断文件或文件夹是否存在
/// </summary>
/// <param name="path">路径</param>
/// <param name="is_directory">true:判断是否是文件夹,false:判断是否是文件</param>
/// <returns>路径的文件或文件夹是否存在</returns>
bool IsExist(const std::filesystem::path& path, bool is_directory)const {
std::error_code error;
auto file_status = std::filesystem::status(path, error);
if (error) {
return false;
}
if (!std::filesystem::exists(file_status)) {
return false;
}
return std::filesystem::is_directory(file_status) && is_directory;
}
bool IsFileExist(const std::filesystem::path& path)const {
return IsExist(path, false);
}
bool IsFileExist(const std::string_view& path)const {
return IsFileExist(std::filesystem::path(path));
}
bool IsFileExist(const std::wstring_view& path)const {
return IsFileExist(std::filesystem::path(path));
}
bool IsFileExist(const wchar_t* path)const {
return IsFileExist(std::wstring_view(path));
}
bool IsFileExist(const char* path)const {
return IsFileExist(std::string_view(path));
}
bool IsDirExist(const std::filesystem::path& path)const {
return IsExist(path, true);
}
bool IsDirExist(const std::string_view& path)const {
return IsDirExist(std::filesystem::path(path));
}
bool IsDirExist(const std::wstring_view& path)const {
return IsDirExist(std::filesystem::path(path));
}
bool IsDirExist(const wchar_t* path)const {
return IsDirExist(std::wstring_view(path));
}
bool IsDirExist(const char* path)const {
return IsDirExist(std::string_view(path));
}
std::filesystem::path::string_type GetCurrentPath()const
{
using std::filesystem::current_path;
return current_path().native();
}
void Emun(const std::filesystem::path& path,bool EnumChild,std::function<bool(const std::filesystem::path&)> callback)const
{
using std::filesystem::directory_iterator;
for (auto& p : directory_iterator(path)) {
if (!callback(p.path()))
break;
if (EnumChild && IsExist(p.path(),true))
{
Emun(p.path(), EnumChild, callback);
}
}
}
};
bool PrintDir(const std::filesystem::path& path)
{
std::cout << path<<std::endl;
return true;
}
int main()
{
CFileSystem filesystem;
std::cout << "-----------------------------filesystem test---------------------------------------" << std::endl;
std::cout << "" << std::endl;
std::cout << "-----------------------------GetCurrentPath---------------------------------------" << std::endl;
std::wcout << filesystem.GetCurrentPath()<<std::endl;
std::cout << "-------------------------------Emun------------------------------------------------" << std::endl;
filesystem.Emun(filesystem.GetCurrentPath(),true, std::bind_front(PrintDir));
std::cout << "------------------------------------------------------------------------------------" << std::endl;
filesystem.Emun(filesystem.GetCurrentPath(), false, std::bind_front(PrintDir));
std::cout << "--------------------------------IsDirExist-----------------------------------------------" << std::endl;
std::cout <<std::boolalpha << filesystem.IsDirExist(LR"(.)") << std::endl;
std::cout << std::boolalpha << filesystem.IsDirExist(".") << std::endl;
std::cout << "--------------------------------IsFileExist-----------------------------------------------" << std::endl;
std::cout << std::boolalpha << filesystem.IsFileExist(LR"(.)") << std::endl;
std::cout << std::boolalpha << filesystem.IsFileExist(".") << std::endl;
std::cout << std::boolalpha << filesystem.IsFileExist(LR"(.\CFileSystem.exe)") << std::endl;
std::cout << std::boolalpha << filesystem.IsFileExist(R"(..\Debug\CFileSystem.exe)") << std::endl;
}