Klasör Kopyala - CSharp


Tam yolu verilen bir klasörü hedef yola kopyalamaya yarayan csharp kodudur.

Kod


public static void KlasorKopyala(string kaynakKlasor, string hedefKlasor, bool altKlasorleriKopyala)
{
    // Get the subdirectories for the specified directory.
    DirectoryInfo dir = new DirectoryInfo(kaynakKlasor);

    if (!dir.Exists)
    {
        throw new DirectoryNotFoundException(
            "Source directory does not exist or could not be found: "
            + kaynakKlasor);
    }

    DirectoryInfo[] dirs = dir.GetDirectories();
    // If the destination directory doesn't exist, create it.
    if (!Directory.Exists(hedefKlasor))
    {
        Directory.CreateDirectory(hedefKlasor);
    }

    // Get the files in the directory and copy them to the new location.
    FileInfo[] files = dir.GetFiles();
    foreach (FileInfo file in files)
    {
        string temppath = Path.Combine(hedefKlasor, file.Name);
        file.CopyTo(temppath, false);
    }

    // If copying subdirectories, copy them and their contents to new location.
    if (altKlasorleriKopyala)
    {
        foreach (DirectoryInfo subdir in dirs)
        {
            string temppath = Path.Combine(hedefKlasor, subdir.Name);
            KlasorKopyala(subdir.FullName, temppath, altKlasorleriKopyala);
        }
    }
}

Etiketler
csharp