函数库.h
// Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Microsoft/AllowMicrosoftPlatformTypes.h"
#include "FileWriteAndRead.generated.h"/*** */
UCLASS()
class SPLINECPP_API UFileWriteAndRead : public UBlueprintFunctionLibrary
{GENERATED_BODY()UFUNCTION(BlueprintCallable,Category = "FileWriteAndRead")static FString ReadFromFile(FString Dir);UFUNCTION(BlueprintCallable,Category = "FileWriteAndRead")static void WriteToFile(FString FileName,FString content);UFUNCTION(BlueprintCallable,Category = "FileWriteAndRead")static TArray<FString> FindFiles(FString Patch,FString Filter,bool Files,bool Directory);UFUNCTION(BlueprintCallable,Category = "FileWriteAndRead")static bool MoveFileTo(FString To,FString From);UFUNCTION(BlueprintCallable,Category = "FileWriteAndRead")static bool DeleteFile(FString FilePath);UFUNCTION(BlueprintCallable,Category = "FileWriteAndRead")static void OpenFile(FString FilePath);UFUNCTION(BlueprintCallable,Category = "FileWriteAndRead")static void CreateDir(FString FilePath);UFUNCTION(BlueprintCallable,Category = "FileWriteAndRead")static void DeleteDic(FString FilePath);};
函数库.cpp
// Fill out your copyright notice in the Description page of Project Settings.#include "FileWriteAndRead.h"
#include "HAL/FileManagerGeneric.h"FString UFileWriteAndRead::ReadFromFile(FString Dir)
{FString Result;FFileHelper::LoadFileToString(Result,*(FPaths::ProjectDir()+Dir));return Result;
}void UFileWriteAndRead::WriteToFile(FString FileName, FString content)
{FFileHelper::SaveStringToFile(content,*(FPaths::ProjectDir()+FileName));
}//查找文件或文件夹
TArray<FString> UFileWriteAndRead::FindFiles(FString Patch, FString Filter, bool Files, bool Directory)
{TArray<FString> FilePathList;FilePathList.Empty();FFileManagerGeneric::Get().FindFilesRecursive(FilePathList,*Patch,*Filter,Files,Directory);return FilePathList;
}//移动文件到其他位置
bool UFileWriteAndRead::MoveFileTo(FString To, FString From)
{return IFileManager::Get().Move(*To,*From);
}//删除文件
bool UFileWriteAndRead::DeleteFile(FString FilePath)
{return IFileManager::Get().Delete(*FilePath);
}//打开文件
void UFileWriteAndRead::OpenFile(FString FilePath)
{FPlatformProcess::CreateProc(*(FPaths::ProjectDir()+FilePath),nullptr,true,false,false,nullptr,0,nullptr,nullptr);
}//新建文件夹
void UFileWriteAndRead::CreateDir(FString FilePath)
{IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
}//删除文件夹
void UFileWriteAndRead::DeleteDic(FString FilePath)
{IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();PlatformFile.DeleteDirectory(*FilePath);
}