删除目录而不是其中的文件

huangapple go评论147阅读模式
英文:

Deletes a directory instead of the files inside it

问题

在存储库文件夹内处理文件和目录时存在问题。问题在于,当您尝试仅删除特定文件夹内的文件时,“UploadedFilesForEncrypting”文件夹本身也被删除。

这是用于用户共享文件夹的字段:

  1. public string FolderForEnc
  2. {
  3. get
  4. {
  5. return Path.Join(_webHostEnvironment.WebRootPath, "UploadedFilesForEncrypting");
  6. }
  7. }

这是控制器本身,其中包含删除用户文件夹中文件的一些逻辑:

  1. [HttpDelete("DeleteEncryptedFiles")]
  2. public IActionResult DeleteFiles([FromHeader(Name = "ApiKey")] string apiKey)
  3. {
  4. try
  5. {
  6. // 在这里进行 API 密钥的验证和用户授权
  7. var userid = "air"; //_readUser.ReadUserIdByApiKey(apiKey)
  8. string uploadsFolder = Path.Join(FolderForEnc, $"User({userid})");
  9. if (Directory.Exists(uploadsFolder))
  10. {
  11. string[] uploadedFiles = Directory.GetFiles(uploadsFolder);
  12. if (uploadedFiles.Length > 0)
  13. {
  14. foreach (string filePath in uploadedFiles)
  15. {
  16. System.IO.File.Delete(filePath);
  17. }
  18. return Ok(new { message = "文件删除成功" });
  19. }
  20. return NotFound("您的文件夹中没有文件");
  21. }
  22. else
  23. {
  24. return NotFound("用户文件夹未找到");
  25. }
  26. }
  27. catch (InvalidOperationException)
  28. {
  29. return BadRequest("未找到具有此 API 密钥的用户");
  30. }
  31. catch (Exception ex)
  32. {
  33. return BadRequest(ex.Message);
  34. }
  35. }

尝试更改Path.JoinPath.Combine,尝试将Directory.GetFiles(uploadsFolder)更改为Directory.GetFiles(uploadsFolder, "*", SearchOption.AllDirectories)。我还尝试删除参数,并像这个示例中传递抽象数据,但没有帮助。

英文:

There is a problem with working with files and directories inside the repository folder. The problem is that when you try to delete only files inside a certain folder, the "UploadedFilesForEncrypting" folder itself is deleted.

Here is the field with shared folder for users

  1. public string FolderForEnc
  2. {
  3. get
  4. {
  5. return Path.Join(_webHostEnvironment.WebRootPath, "UploadedFilesForEncrypting");
  6. }
  7. }

Here is the controller itself in which there is a small logic for deleting files in the user folder

  1. [HttpDelete("DeleteEncryptedFiles")]
  2. public IActionResult DeleteFiles([FromHeader(Name = "ApiKey")] string apiKey)
  3. {
  4. try
  5. {
  6. // ЗДЕСЬ ПРОВЕРКА ПОДДЛИННОСТИ API КЛЮЧА И АВТОРИЗАЦИЯ ПОЛЬЗОВАТЕЛЯ
  7. var userid = "air"; //_readUser.ReadUserIdByApiKey(apiKey)
  8. string uploadsFolder = Path.Join(FolderForEnc, $"User({userid})");
  9. if (Directory.Exists(uploadsFolder))
  10. {
  11. string[] uploadedFiles = Directory.GetFiles(uploadsFolder);
  12. if(uploadedFiles.Length > 0)
  13. {
  14. foreach (string filePath in uploadedFiles)
  15. {
  16. System.IO.File.Delete(filePath);
  17. }
  18. return Ok(new { message = "Files deleted successfully" });
  19. }
  20. return NotFound("There are no files in your folder");
  21. }
  22. else
  23. {
  24. return NotFound("User folder not found");
  25. }
  26. }
  27. catch (InvalidOperationException)
  28. {
  29. return BadRequest("User with this API key not found");
  30. }
  31. catch (Exception ex)
  32. {
  33. return BadRequest(ex.Message);
  34. }
  35. }

Tried changing Path.Join to Path.Combine, tried changing Directory.GetFiles(uploadsFolder) to Directory.GetFiles(uploadsFolder, "*", SearchOption.AllDirectories). I also tried to remove parameters and pass abstract data as in this example.
None of this helped.

答案1

得分: 1

结果是什么,有没有错误?据我所知,File.Delete 如果文件不存在是不会抛出任何错误的。

  • 如果您确定路径正确,请检查文件夹的权限以执行删除操作。
  • 请确保您输入的文件扩展名是正确的。
  • 最后一种可能是文件仍然被某个后台进程使用。在这种情况下,它不会失败,但也不会删除文件。
英文:

What is the result, is there any error? As I know, File.Delete does not throw any error if file is not exists.

  • If you are certain about the path, please check folder's permission
    to delete operations.
  • Please ensure that the file extensions are correct which you put.
  • Last option is that the file is still in use by some background
    process. Then it does not fail but it does not delete the file.

huangapple
  • 本文由 发表于 2023年8月10日 21:12:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876058.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定