CryptoJones
CryptoJones

Reputation: 745

Is Directory.EnumerateFiles locking the file it is trying to copy?

I am trying to write a program to copy my SQL Database Backup files to a NAS. It was working before, but then I added in checking to see if the paths exist and now when it tries to copy the file it gets an exception saying "System.IO.IOException: The process cannot access the file 'C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup\AdventureWorks2012.bak' because it is being used by another process."

I've checked process explorer and no other program is touching the file.

What am I doing wrong?

Code Below

namespace BackupCopy
{
    class Program
    {
        static void Main(string[] args)
        {
            // Set Source Path
            string sourcePath = @"C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup";

            // Verify Source Path Exists
            if (!Directory.Exists(sourcePath))
            {
                Console.WriteLine("Source path does not exist.");
                Console.ReadLine();
                return;
            }



            // Backup each file in Array
            foreach (string filename in Directory.EnumerateFiles(sourcePath))
            {
                Backup(filename);
            }

        }

        public static void Backup(string filename)
        {
            // Pull filename from method input parameter
            string fileName = filename;

            // Set Paths
            string sourcePath = @"C:\Program Files\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQL\Backup";
            string targetPath = @"\\prometheus\NAS\DB Backups\mercurys";

            // Verify Target Path Exists
            if (!Directory.Exists(targetPath))
            {
                Console.WriteLine("Target path does not exist.");
                Console.ReadLine();
                return;
            }

            // Use Path class to manipulate file and directory paths. 
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);

            // To copy a file to another location and  
            // overwrite the destination file if it already exists.
            try
            {
                System.IO.File.Copy(sourceFile, destFile, true);
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc);
                Console.ReadLine();
                return;
            }
        }
    }
}

Upvotes: 3

Views: 2349

Answers (1)

CryptoJones
CryptoJones

Reputation: 745

The problem is the program is locking the file-handle when it enumerates the file in the directory.

Replacing this;

        // Backup each file in Array
        foreach (string filename in Directory.EnumerateFiles(sourcePath))
        {
            Backup(filename);
        }

With this;

        Backup("AdventureWorks2012.bak");

Solves the problem.

Upvotes: 1

Related Questions