Exemple #1
0
        /// <summary>
        /// Copies an existing file. Overwrites an existing file if <paramref name="overwrite"/> is true
        /// </summary>
        /// <param name="sourceFileName">The file to copy.</param>
        /// <param name="targetDirectory">Target directory</param>
        /// <param name="newFileName">New File name. Null or empty to use <paramref name="sourceFileName"/>'s name</param>
        /// <param name="overwrite">true to overwrite existing file</param>
        /// <remarks>http://msdn.microsoft.com/en-us/library/c6cfw35a(v=vs.110).aspx</remarks>
        /// <exception cref="FileSystemIsBusyException">Filesystem is busy</exception>
        public static void CopyToDirectory(string sourceFileName, string targetDirectory, String newFileName = null, Boolean overwrite = false)
        {
            Contract.Requires(!String.IsNullOrWhiteSpace(sourceFileName));
            Contract.Requires(!String.IsNullOrWhiteSpace(targetDirectory));

            if (String.IsNullOrWhiteSpace(sourceFileName))
            {
                throw new ArgumentNullException(nameof(sourceFileName));
            }
            if (String.IsNullOrWhiteSpace(targetDirectory))
            {
                throw new ArgumentNullException(nameof(targetDirectory));
            }

            // determine filename
            string targetFileName;

            if (!String.IsNullOrWhiteSpace(newFileName))
            {
                // TODO: Check for invalid chars
                targetFileName = newFileName;
            }
            else
            {
                targetFileName = QuickIOPath.GetName(sourceFileName);
            }

            Copy(sourceFileName, QuickIOPath.Combine(targetDirectory, targetFileName), overwrite);
        }