Click here to Skip to main content
15,889,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,i called file.copy to copy file and folder from source to destination. When i run it without debugger the File.copy did not copy everything to destination. When I debug the program with debugger the code works like a charm, anyone have any idea why the file.copy do not copy everything without debugger? Below is the code:
C#
public bool CopyFileAndFolder(string sourceFolder, string replacePath)
{
    bool result = false;
    try
    {
        foreach (string extractPath in Directory.GetDirectories(sourceFolder, "*", SearchOption.AllDirectories))
        {
            string destFolder = extractPath.Replace(sourceFolder, replacePath);
            if (!Directory.Exists(destFolder))
                Directory.CreateDirectory(destFolder);
        }

        foreach (string extractFile in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
        {
            string destFile = extractFile.Replace(sourceFolder, replacePath);
            File.Copy(extractFile, destFile, true);
        }
        result = true;

    }
    catch (Exception ex)
    {
        result = false;
        Trace.WriteLine(ex.Message);
    }
    return result;
}
Posted
Updated 16-Nov-15 16:32pm
v2
Comments
Garth J Lancaster 16-Nov-15 22:38pm    
I'd check for two things
a) that you're getting properly formed filenames when you form 'destFile'
b) that you're not running into permissions errors - are you running VS under admin ? - try running the exe as Admin outside of VS and see if you get the same result
Soon Ee 16-Nov-15 22:52pm    
yeah i have method to check for the file path format and permission

1 solution

You could try to change the code like this
C#
public bool CopyFileAndFolder(string sourceFolder, string replacePath)
{
    bool result = false;
    try
    {
        foreach (string extractPath in Directory.GetDirectories(sourceFolder, "*", SearchOption.AllDirectories))
        {
            string destFolder = extractPath.Replace(sourceFolder, replacePath);
            if (!Directory.Exists(destFolder))
                Directory.CreateDirectory(destFolder);

            foreach (string extractFile in Directory.GetFiles(sourceFolder, "*.*", SearchOption.AllDirectories))
            {
                string destFile = Path.Combine(destFolder, Path.GetFileName(extractFile));
                File.Copy(extractFile, destFile, true);
            }      
        }
 
        result = true;
 
    }
    catch (Exception ex)
    {
        result = false;
        Trace.WriteLine(ex.Message);
    }
    return result;
}

That is, move the file copy loop into the directory search loop and change the destination path a bit.

[UPDATE]
I found it easier to make this method recursive instead. As far as I can tell it works for me.
C#
// Recursive version
public bool CopyFileAndFolder(string sourceFolder, string backupPath)
{
    bool retVal = false;
    try
    {
        // First create the new backup directory
        if (!Directory.Exists(backupPath))
            Directory.CreateDirectory(backupPath);


        // Then copy all the files in this directory
        foreach (string fileName in Directory.GetFiles(sourceFolder, "*.*", SearchOption.TopDirectoryOnly))
        {
            string destFile = Path.Combine(backupPath, Path.GetFileName(fileName));
            File.Copy(fileName, destFile, true);
        }

        // Get the sub-diretories
        string[] directories = Directory.GetDirectories(sourceFolder, "*", SearchOption.TopDirectoryOnly);
        // This check is just to make the recursion break point more clear
        if (directories.Length == 0)
            return true;

        foreach (string directory in directories)
        {
            string destFolder = Path.Combine(backupPath, Path.GetFileName(directory));
            retVal = CopyFileAndFolder(directory, destFolder);
            if (!retVal)
                break;
        }
    }
    catch (Exception ex)
    {
        retVal = false;
        Trace.WriteLine(ex.Message);
    }
    return retVal;
}
 
Share this answer
 
v2
Comments
Soon Ee 16-Nov-15 22:59pm    
Sir i tried you solution, first time the result improved, but still not copying all files, second times onward the program behaves like before. Thanks anyway
George Jonsson 16-Nov-15 23:01pm    
From where do you call this method?
From different threads or so?
Soon Ee 16-Nov-15 23:24pm    
i use another method to call the method above,FYI, i will delete the destination folder first, andthen i will create a new one with same folder name and start the copy process, below is the caller function
private bool StartFileRollBackProcess()
{
bool result = false;

string backupFolder = Path.Combine(ConfigurationManager.AppSettings["BackupPath"], completeVersionNumber);
string destBackUpFolder = Directory.GetParent(iisConf.PhysicalPath).FullName;

try
{
DirectoryInfo folderToBeDelete = new DirectoryInfo(destBackUpFolder);
folderToBeDelete.Delete(true);

if (Directory.Exists(backupFolder))
{
Directory.CreateDirectory(destBackUpFolder);
//result = CopyAll(new DirectoryInfo(backupFolder), new DirectoryInfo(destBackUpFolder));
result = CopyFileAndFolder(backupFolder, destBackUpFolder);

if (result)
{
ErrorMsg = "Copy process Failed,Your File has rolled back to previous version";
IsErrorDetected = true;
}
}
}
catch (Exception ex)
{
ErrorMsg = ex.Message+"\n"+ex.StackTrace+"\n"+ex.InnerException;
IsErrorDetected = true;
}

return result;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900