Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi Folks Thanks to djones7936 on

Status
Not open for further replies.

Kenny100

Technical User
Feb 6, 2001
72
NZ
Hi Folks

Thanks to djones7936 on this forum I've got a nice sub routine that copies all of the files and subdirectories within a directory to another directory. The thing is, I'm having a bit of trouble implementing the subroutine. I'm a bit new to C# so I've been reading like mad but I still can't see where I'm going wrong:

using System;

public class FileCopier
{
public static void Main()
{
CopyAllFiles("C:\\test\\test1A", "C:\\test\\test2A", true);
}

private void CopyAllFiles(string strSourceDirectory, string strDestinationDirectory, bool bCheckSubdir)
{
// Code contained here thanks to djone7936
}
}

When I try to call the subroutine like this I get the following error:

An object reference is required for the nonstatic field, method, or property 'FileCopier.CopyAllFiles(string, string, bool)'

What glaring error have I made here?!

Cheers,
Kenny.
 
You're going from a static method (Main) to a instance method (CopyAllFiles), and instance methods require an object instance.

Move your file copy routine into another class named KFile. In your Main, you'd then do something like:
Code:
KFile MyKFile = new KFile();   // create instance

MyKFile.CopyAllFiles("c:\foo", "c:\bar");

Chip H.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top