gpt4 book ai didi

c# - dotnet 核心 File.Move 问题

转载 作者:行者123 更新时间:2023-11-30 17:25:13 24 4
gpt4 key购买 nike

我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com