英文:
Deletes a directory instead of the files inside it
问题
在存储库文件夹内处理文件和目录时存在问题。问题在于,当您尝试仅删除特定文件夹内的文件时,“UploadedFilesForEncrypting”文件夹本身也被删除。
这是用于用户共享文件夹的字段:
public string FolderForEnc
{
get
{
return Path.Join(_webHostEnvironment.WebRootPath, "UploadedFilesForEncrypting");
}
}
这是控制器本身,其中包含删除用户文件夹中文件的一些逻辑:
[HttpDelete("DeleteEncryptedFiles")]
public IActionResult DeleteFiles([FromHeader(Name = "ApiKey")] string apiKey)
{
try
{
// 在这里进行 API 密钥的验证和用户授权
var userid = "air"; //_readUser.ReadUserIdByApiKey(apiKey)
string uploadsFolder = Path.Join(FolderForEnc, $"User({userid})");
if (Directory.Exists(uploadsFolder))
{
string[] uploadedFiles = Directory.GetFiles(uploadsFolder);
if (uploadedFiles.Length > 0)
{
foreach (string filePath in uploadedFiles)
{
System.IO.File.Delete(filePath);
}
return Ok(new { message = "文件删除成功" });
}
return NotFound("您的文件夹中没有文件");
}
else
{
return NotFound("用户文件夹未找到");
}
}
catch (InvalidOperationException)
{
return BadRequest("未找到具有此 API 密钥的用户");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
尝试更改Path.Join
为Path.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
public string FolderForEnc
{
get
{
return Path.Join(_webHostEnvironment.WebRootPath, "UploadedFilesForEncrypting");
}
}
Here is the controller itself in which there is a small logic for deleting files in the user folder
[HttpDelete("DeleteEncryptedFiles")]
public IActionResult DeleteFiles([FromHeader(Name = "ApiKey")] string apiKey)
{
try
{
// ЗДЕСЬ ПРОВЕРКА ПОДДЛИННОСТИ API КЛЮЧА И АВТОРИЗАЦИЯ ПОЛЬЗОВАТЕЛЯ
var userid = "air"; //_readUser.ReadUserIdByApiKey(apiKey)
string uploadsFolder = Path.Join(FolderForEnc, $"User({userid})");
if (Directory.Exists(uploadsFolder))
{
string[] uploadedFiles = Directory.GetFiles(uploadsFolder);
if(uploadedFiles.Length > 0)
{
foreach (string filePath in uploadedFiles)
{
System.IO.File.Delete(filePath);
}
return Ok(new { message = "Files deleted successfully" });
}
return NotFound("There are no files in your folder");
}
else
{
return NotFound("User folder not found");
}
}
catch (InvalidOperationException)
{
return BadRequest("User with this API key not found");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论