Copies a file from one path to another, while creating any parent directories if they don't already exist. - CSharp System.IO

CSharp examples for System.IO:File Path

Description

Copies a file from one path to another, while creating any parent directories if they don't already exist.

Demo Code

//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using System.IO;/*from  w  ww.  j av  a2 s .c  o m*/

public class Main{
        /// <summary>
        /// Copies a file from one path to another, while creating any parent directories if they don't already exist.
        /// </summary>
        /// <param name="source">Path to the file to copy.</param>
        /// <param name="destination">Path to the copied file.</param>
        public static void Copy(string source, string destination)
        {
            string destParent = PathEx.GetParent(destination);
            if (!string.IsNullOrEmpty(destParent))
            {
                if (!Directory.Exists(destParent))
                    Directory.CreateDirectory(destParent);
            }

            File.Copy(source, destination);
        }
}

Related Tutorials