Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dotnet core File.Move issue

My Dockerfile is this

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"]

and I build image with this command

docker build -t watcher/sl_user_test:1.1 .

so, my docker-compose is this

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

In my dotnet core app I get a file in the /in folder and I move it to /tmp/aaa code is this

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

the problem is that this command copy file and doesn't move it, why? If I go inside the container I can do mv from bash and it works

like image 324
Mauro Sala Avatar asked Feb 17 '26 01:02

Mauro Sala


1 Answers

Since you are creating a new GUID in the path, the directory is guaranteed to not exist already. Thus File.Move will throw a DirectoryNotFoundException.

Create it before you move the file.

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

Assuming you are doing that, if the File.Move method just does a copy instead of a move, then it is likely that the original file is in use. From the docs (in the Remarks section):

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.

like image 113
Matt Johnson-Pint Avatar answered Feb 19 '26 14:02

Matt Johnson-Pint



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!