Question

I'm trying to display the current file being copied in a method.

Here is the code:

public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
    // Check if the target directory exists, if not, create it.
    if (Directory.Exists(target.FullName) == false)
        Directory.CreateDirectory(target.FullName);

    // Copy each file into it’s new directory.
    foreach (FileInfo fi in source.GetFiles())
    {
        //Line where the error is
        lblBackupStatus.Text = "Copying " + target.FullName.ToString() + fi.Name.ToString(); 

        fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
    }

    // Copy each subdirectory using recursion.
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
    {
        DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
        CopyAll(diSourceSubDir, nextTargetSubDir);
    }
}

The error I'm getting is :

An object reference is required for the non-static field, method, or property

Something about calling a non static property from a static method but I'm not sure how to fix it.

Any help is appreciated! , Thanks

Was it helpful?

Solution

My guess is you're probably getting the error on "lblBackupStatus.Text = ....

The label belongs to a class instance (an "object"); your static CopyAll method doesn't (it's "static").

Q: Does CopyAll need to be static? If not, see if eliminating "static" fixes the problem.

Otherwise, please post/identify the line that's giving the error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top