Moves 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

Moves 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  w  w. j  a  v a2  s . c o  m*/

public class Main{
        /// <summary>
        /// Moves 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 move.</param>
        /// <param name="destination">New location and/or name of the file.</param>
        public static void Move(string source, string destination)
        {
            string destParent = PathEx.GetParent(destination);
            if (!string.IsNullOrEmpty(destParent))
            {
                if (!Directory.Exists(destParent))
                    Directory.CreateDirectory(destParent);
            }

            File.Move(source, destination);
        }
}

Related Tutorials