c# - dotnet 核心 File.Move 问题

标签 c# docker .net-core

我的 Dockerfile 是这样的

FROM mcr.microsoft.com/dotnet/core/runtime:3.0

COPY publish/ app/
RUN mkdir -p /in
RUN mkdir -p /tmp

ENTRYPOINT ["dotnet", "app/Watcher.dll"]

然后我用这个命令构建镜像

docker build -t watcher/sl_user_test:1.1 .

所以,我的 docker-compose 是这样的

watcher.sl_user_test:
  image: watcher/sl_user_test:1.1
  container_name: watcher_sl_user_test
  build: .
  restart: always
  volumes:
    - /var/storage/in:/in:z
    - /var/storage/tmp:/tmp:z

在我的 dotnet 核心应用程序中,我在/in 文件夹中获取了一个文件,并将其移动到/tmp/aaa 代码是这样的

string destination = $"/tmp/{Guid.NewGuid()}/git.zip";
new FileInfo(destination).Directory.Create();
File.Move("/in/git.zip", destination, true);

问题是这个命令复制文件而不移动它,为什么? 如果我进入容器内部,我可以从 bash 执行 mv 并且它有效

最佳答案

由于您正在路径中创建新的 GUID,因此可以保证该目录不存在。因此 File.Move 将抛出一个 DirectoryNotFoundException

在移动文件之前创建它。

var tmpDir = $"/tmp/{Guid.NewGuid()}";
Directory.CreateDirectory(tmpDir);
File.Move("/in/git.zip", $"{tmpDir}/git.zip", true);

假设您正在这样做,如果 File.Move 方法只是复制而不是移动,那么很可能原始文件正在使用中。来自 the docs (在备注部分):

If you try to move a file across disk volumes and that file is in use, the file is copied to the destination, but it is not deleted from the source.

关于c# - dotnet 核心 File.Move 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58978607/

相关文章:

java - 在 C# 中序列化 Java 对象和反序列化

c# - aspnet核心无法解析依赖关系: error :'Unable to resolve service for type while attempting to activate'

bash - 从Docker容器A(在Docker容器B上)运行Bash脚本

docker - Bazel:将运行文件包含在container_image中

xunit.net - 如何使用 dotnet test 运行特定测试?

c# - 如何让 .NET Core 项目将 NuGet 引用复制到构建输出?

c# - 佳能图像处理 SDK 示例

c# - 我的 InvokeRequied #2 有什么问题?

docker - 在 Docker 中链接多个容器

c# - 如何在 .NET Core 3.1.0 中使用 ACL 在 Mac OS 上管理文件夹权限