Hello,

I'm having this issue with copying files with .NET's File.Copy() method.

I have a file, that I want to copy to a couple locations, but when doing something like:

File.Copy(source, target1);
File.Copy(source, target2);

I keep getting an exception error about the file being in use.

Seeing that if I copy it to just 1 location, wait a few moments and then copy it again, it works fine, I'm assuming that the error presented to me isn't because of the Copy() method not closing the file correctly, but rather because I'm trying to use the second copy while the first one is still being processed.

My question, then, would be, how should I go about this?
I couldn't find a method for checking if a file is currently accessed.

Would it be efficient to loop the 2nd copy in a try-catch block?


Just curious as to what others have done in a similiar situation.

Thanks for the answers to come!

Recommended Answers

All 4 Replies

If you create a delay between the copy functions, does it work?

I don't want to rely on delays, since I can't always precisely tell the time the copy will take.

As for the try method, I tried doing a try-finally loop like this:

File::Copy(source, target1);
bool copyDone = false;
while(!copyDone){
	try{
		File::Copy(source, target2);
	}
	finally{
		copyDone = true;
	}
}

But that doesn't seem to work either, it goes to the finally scope on the first go, and still crashes for some reason.

A better technique would then be to create your own copy method -- either by writing the bytes or calling a static method that calls copy:

using System;
using System.IO;

namespace DW_398939
{
   public class CFileCopy
   {
      public static string strError { get; set; }

      public static bool CopyFile(string strSource, string strDest)
      {
         bool blnRetVal = true;
         try
         {
            File.Copy(strSource, strDest);
         }
         catch (Exception exc)
         {
            blnRetVal = false;
            strError = exc.Message;
         }

         return blnRetVal;
      }

      public static bool CopyFile(string strSource, string strDest, bool overwrite)
      {
         bool blnRetVal = true;
         try
         {
            File.Copy(strSource, strDest, overwrite);
         }
         catch (Exception exc)
         {
            blnRetVal = false;
            strError = exc.Message;
         }

         return blnRetVal;
      }
   }
}

Can then be called by:

using System;

namespace DW_398939
{
   class Program
   {
      static void Main(string[] args)
      {
         if (!CFileCopy.CopyFile("c:/science/FRED.txt", "c:/science/FRED1.txt"))
         {
            Console.WriteLine(CFileCopy.strError);
            return;
         }

         if (!CFileCopy.CopyFile("c:/science/FRED1.txt", "c:/science/FRED2.txt"))
         {
            Console.WriteLine(CFileCopy.strError);
            return;
         }
      }
   }
}

I tried this on a 38MB file in the same directory and it was a success.
I used the same file name the first time, then the copied file name the second time JUST to make sure it could not copy until the file had arrived.

Weird... That actually worked.

I don't really see the difference between your code and mine, but it still worked when I put it all into a function and called upon it.

Thanks alot ._.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.