public void Delete(int id) { using var trans = repoService.DbContext.Database.BeginTransaction(); var dbBackupEntity = repoService.FindOne(id); if (dbBackupEntity != null) { hostingEnvironment.DeleteFile(dbBackupEntity.FilePath); repoService.Delete(dbBackupEntity); } trans.Commit(); }
/// <summary> /// 将文件移动到指定目录 /// </summary> /// <param name="env"></param> /// <param name="sourceFilePath">需要移动的源文件的绝对路径</param> /// <param name="descDirectoryPath">移动到的目录的绝对路径</param> public static void Move(this IHostingEnvironment env, string sourceFilePath, string descDirectoryPath) { //获取源文件的名称 string sourceFileName = Path.GetFileName(sourceFilePath); if (IsExistDirectory(descDirectoryPath)) { var destFilePath = Path.Join(descDirectoryPath, sourceFileName); //如果目标中存在同名文件,则删除 if (IsExistFile(destFilePath)) { env.DeleteFile(destFilePath); } //将文件移动到指定目录 File.Move(sourceFilePath, destFilePath); } }
/// <summary> /// 清空指定目录下所有文件及子目录,但该目录依然保存. /// </summary> /// <param name="env"></param> /// <param name="directoryPath">指定目录的绝对路径</param> public static void ClearDirectory(this IHostingEnvironment env, string directoryPath) { directoryPath = Path.Combine(env.ContentRootPath, directoryPath); if (IsExistDirectory(directoryPath)) { //删除目录中所有的文件 string[] fileNames = GetFileNames(directoryPath); foreach (var fileName in fileNames) { env.DeleteFile(fileName); } //删除目录中所有的子目录 string[] directoryNames = GetDirectories(directoryPath); foreach (var directoryName in directoryNames) { env.DeleteDirectory(directoryName); } } }