Ciao a tutti
mi trovo un po in difficoltà con un programma che sto sviluppando.
Alla pressione di un bottone, viene avviato questo semplice programma programato in modalità console, che deve copiare una directory con tutti i file contenuti e le sottodirectory da un percorso sorgente a un percorso di destinazione.
Il mio problema è che non capisco perchè all'avvio il programma mi vengono copiati solo i file e non le sottocartelle contenute nella directory che vado a selezionare, mentre io ho bisogno di copiare ogni cosa, file cartelle sottocartelle e file contenuti nelle sottocartelle.
Se potreste aiutarmi,
grazie in anticipo
Matteo.
static void Main(string[] args)
{
string SourceFolder=Directory.GetCurrentDirectory() + @"\DACOPIARE";
//directory sorgente
string TargetFolder =@"C:\MyFolder\COPIATA";
//direcotry destinazione
Console.WriteLine("Avvio Copia File");
Console.WriteLine("Copia File in Corso");
Console.WriteLine("Attendere...");
string fileName = "testation.share";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(SourceFolder, fileName);
string destFile = System.IO.Path.Combine(TargetFolder, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(TargetFolder))
{
System.IO.Directory.CreateDirectory(TargetFolder);
}
// To copy a file to another location and
// overwrite the destination file if it already exists.
//System.IO.File.Copy(sourceFile, destFile, true);
// To copy all the files in one directory to another directory.
// Get the files in the source folder. (To recursively iterate through
// all subfolders under the current directory, see
// "How to: Iterate Through a Directory Tree.")
// Note: Check for target path was performed previously
// in this code example.
if (System.IO.Directory.Exists(SourceFolder))
{
string[] files = System.IO.Directory.GetFiles(SourceFolder);
// Copy the files and overwrite destination files if they already exist.
foreach (string s in files)
{
Console.WriteLine("Copia di " + System.IO.Path.GetFileName(s) + " in corso..");
// Use static Path methods to extract only the file name from the path.
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(TargetFolder, fileName);
System.IO.File.Copy(s, destFile, true);
Console.WriteLine(System.IO.Path.GetFileName(s) + " trasferito");
//scrive che il file è stato copiato
}
}
else
{
Console.WriteLine("Errore sulla cartella sorgente!");
}
// Keep console window open in debug mode.
Console.WriteLine("Premere un Tasto per Terminare..");
Console.ReadKey();
}