how can I call a class to run from program.cs in my form.cs
I am having trouble getting my class to from program.cs to run in form.cs when I request it to. I have tried numbers methods but they don't seem to work, I have used the method that I have used to send variable across between the two files and that didn't work and I have searched but don't understand what the code is doing or where to put it. What I want it to do is run that class in the backgroundworker but the variable method I was shown before doesn't work for the class.
here is the class I want to run
public static class DirectoryInfoExtensions
{
//Copies all files from one directory to another.
public static void CopyTo(this DirectoryInfo source, string destDirectory, bool recursive)
{
if (source == null)
throw new ArgumentNullException("source");
if (destDirectory == null)
throw new ArgumentNullException("destDirectory");
// If the source doesn't exist, we have to throw an exception.
if (!source.Exists)
throw new DirectoryNotFoundException("Source directory not found: " + source.FullName);
// Compile the target.
DirectoryInfo target = new DirectoryInfo(destDirectory);
//If the target doesn't exist, we create it.
if (!target.Exists)
target.Create();
// Get all files and copy them over.
foreach (FileInfo file in source.GetFiles())
{
file.CopyTo(Path.Combine(target.FullName, file.Name), true);
}
// Return if no recursive call is required.
if (!recursive)
return;
// Do the same for all sub directories.
foreach (DirectoryInfo directory in source.GetDirectories())
{
CopyTo(directory, Path.Combine(target.FullName, directory.Name), recursive);
}
}
}
here is where I want to run it in form.cs
private void backgroundWorker1_DoWork(object sender, DoWorkEve开发者_如何学JAVAntArgs e)
{
}
You just want to run the CopyTo method? That should be easy enough. You can either invoke it explicitly:
DirectoryInfoExtensions.CopyTo(source, "C:\DestinationDirectory", true);
... or as an extension method:
source.CopyTo("C:\DestinationDirectory", true);
Of course, if your extensions are not in the same namespace as your form class, you'll need to make sure that you are using the namespace that DirectoryInfoExtensions is in, by placing something like this at the top of your file:
using Utilities; // replace Utilities with whatever namespace DirectoryInfoExtensions is in
And you'll need to have a source
variable of type DirectoryInfo.
var source = new DirectoryInfo("C:\SourceDirectory");
This should work:
DirectoryInfo.CopyTo("<source folder>, "<target folder>", true)
Does it not work this way? What error are you seeing?
精彩评论