Återkalla en metod?
Har fastnat med en liten grej i mitt program. Mitt program gör så att jag kopierar en mapp till en annan plats.
Felet är när jag försöker återkalla en metod som innehåller två argument. Men hur jag ska få in dem utan att få fel meddelande. Jag har testat att skriva:
switch (meny)
{
case "1":
CopyAll(DirectoryInfo source, DirectoryInfo target);
Main();
Console.ReadLine();
break;
case "2":
break;
}
Men det funkar inte, att skriva
CopyAll(source, target);
funkar inte heller
Hur ska jag gå till väga?
Här är koden för programmet:
using System;
using System.IO;
class CopyDir
{
public static void Start()
{
Console.WriteLine("1. Starta backup\n2. Avsluta");
string meny = Console.ReadLine();
switch (meny)
{
case "1":
CopyAll();
Main();
Console.ReadLine();
break;
case "2":
break;
}
}
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
if (source.FullName.ToLower() == target.FullName.ToLower())
{
return;
}
// 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())
{
Console.WriteLine(@Copying {0}\{1}, target.FullName, fi.Name);
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);
}
}
public static void Main()
{
string sourceDirectory = @C:\Users\Adam\Desktop\source;
string targetDirectory = @C:\Users\Adam\Desktop\path;
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
CopyAll(diSource, diTarget);
}
// Output will vary based on the contents of the source directory.
}
Felmeddelandet som poppar upp är: "No overload for method 'CopyAll' takes 0 arguments"