0

I am quite an amateur programmer and this is my first question on Stack Overflow, I hope I am doing everything right :). But maybe someone here can think of what the problem could be.

My program is supposed to move various files from one disk to another on the server. With a large zip file (always about 10-20GB in size) I always have the problem that the following exception is thrown:

System.IO.IOException: The process cannot access the file because it is being used by another process.
   at System.IO.FileSystem.MoveFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.FileInfo.MoveTo(String destFileName, Boolean overwrite)
   at MPOS_Archiver.Program.Main(String[] args) in C:\Users\ivan.gazic\Desktop\MPOS_Archiver\MPOS_Archiver\Program.cs:line 106

Importend to know. I am not creating the files in my Code they are just there on the disk (there is a share folder on the disk where my colleagues are leaving the files) I just want to move them. This is the only part of my code where the zip files are mentiond. So I guess I am not opening a FileStream and there for I don't need to close one. In a similar question on Stack Overflow its always about opening and closing the file streams but I don't see that applying in this case

My program is also running on a test server where the files are much smaller. This problem does not occur there! The code with which I realize the file shift:

files = Directory.GetFiles(Path.Combine(sourcePath, bkz)).ToArray();
if (files.Length > 0)
{
    foreach (string file in files)
    {
        FileInfo dbZIPfile = new FileInfo(file);
        try
        {
            dbZIPfile.MoveTo(Path.Combine(targetPath, bkz, dbZIPfile.Name), true);
        }
        catch (IOException ex)
        {
            Console.WriteLine("There was an error moving the DB ZIP: " + ex);
        }
        Console.WriteLine("File " + dbZIPfile.Name + " moved to " + Path.Combine(targetPath, bkz));
    }
}
else
{
    Console.WriteLine("No DB .ZIP in market" + bkz);
}
L8 ReaperX
  • 11
  • 3
  • Oh yes I should also add the info, that when I reboot the server and then run it one more time it works without problems – L8 ReaperX Sep 07 '22 at 09:31
  • The error explains what's wrong. Some other process locked the file. This has nothing to do with the ZIP format. How is this file created? If it's your own code, did you remember to close the output stream? – Panagiotis Kanavos Sep 07 '22 at 09:49
  • "Some other process locked the file"... or you yourself have opened the file elsewhere in the code and failed to close/dispose of it. – spender Sep 07 '22 at 09:52
  • @PanagiotisKanavos I hope that isn't a too silly question but how can I close the output stream? I don't think I am opening it at all, i mean it works when I reboot the server, or on the test server where the files are much smaller the exception does not appear at all – L8 ReaperX Sep 07 '22 at 10:19
  • @spender The Zip file is Stored in a path like this: D:/Mpos/10/db.zip but folder 10 has also some subfolder so I am creating the files array to get only the zip file or in some cases zip files. Once I have the array and it contais something i run it in the foreach to move all the files using fileinfo to get the name of the file (its every day a diffrrent name) thats all i am doing with the zip files, they are no where else mentiond in the code. So i am not opening the file i guess? or is something in the code posted opening a file stream? Sorry if this question is stupid I am quit a noob :D – L8 ReaperX Sep 07 '22 at 10:33

1 Answers1

0

Some process is getting a handle on your files. You have to identify it. I personnaly use Process Explorer for that kind of task

Maybe if you use visual studio it could be it that keep an handle. Did you try to stop and restart VS ? or whatever IDE you're using

Olivier Duhart
  • 362
  • 3
  • 14