Directory copy

In this chapter you will learn:

  1. How to copy a directory

How to copy a directory

using System;/*from j  av a  2 s .c o  m*/
using System.IO;
using System.Windows.Forms;
using System.Text;

class Util
{
    public static void CopyDirectory(string src, string dst, bool overwrite)
    {
        Directory.CreateDirectory(dst);
        foreach (string dir in Directory.GetDirectories(src))
        {
            CopyDirectory(dir, Path.Combine(dst, Path.GetFileName(dir)), overwrite);
        }
        foreach (string file in Directory.GetFiles(src))
        {
            string dstFile = Path.Combine(dst, Path.GetFileName(file));
            File.Copy(file, dstFile, true);
        }
    }
}

Next chapter...

What you will learn in the next chapter:

  1. How to list all drives
  2. Get all logical drives
Home » C# Tutorial » File, Path
File creation
Deleting Files
Put to recycle bin
File Exists
File moving
File size
File attribute
Set file attribute
File time change
File time
Random file name
Temporary file
Path
Directory creation
Delete directory
Path existance
Change the path
Current Path
Directory listing
Recursive Path Listing
Combine path
Calculate Directory Size
Invalid path characters
System path
Directory copy
Drive
Drive information
File access rule
File System watcher