Exemple #1
0
        /// <summary>
        /// Copies an existing file. Overwrites an existing file if <paramref name="overwrite"/>  is true
        /// </summary>
        /// <param name="source">The file to copy.</param>
        /// <param name="target">Target file</param>
        /// <param name="overwrite">true to overwrite existing files</param>
        /// <param name="createRecursive">Creates parent path if not exists. Decreases copy performance</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 Copy(string source, string target, Boolean overwrite = false, Boolean createRecursive = true)
        {
            Contract.Requires(!String.IsNullOrWhiteSpace(source));
            Contract.Requires(!String.IsNullOrWhiteSpace(target));

            if (String.IsNullOrWhiteSpace(source))
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (String.IsNullOrWhiteSpace(target))
            {
                throw new ArgumentNullException(nameof(target));
            }

            if (createRecursive)
            {
                var targetDirectoryPath = QuickIOPath.GetDirectoryName(target);
                try
                {
                    QuickIODirectory.Create(targetDirectoryPath, true);
                }
                catch (PathAlreadyExistsException)
                {
                    // yay ignore this!
                }
            }

            int win32Error;

            if (!QuickIOEngine.CopyFile(source, target, out win32Error, overwrite))
            {
                Win32ErrorCodes.NativeExceptionMapping(!Exists(source) ? target : target, win32Error);
            }
        }
        /// <summary>
        /// Returns an enumerable collection of directory names.
        /// </summary>
        /// <param name="pattern">Search pattern. Uses Win32 native filtering.</param>
        /// <param name="searchOption">Specifiy depth with <see cref="SearchOption"/></param>
        /// <param name="pathFormatReturn">Type of return</param>
        /// <param name="enumerateOptions">Options <see cref="QuickIOEnumerateOptions"/></param>
        /// <returns>An enumerable collection of the full names (including paths) for the directories in the directory specified by path.</returns>
        public IEnumerable <string> EnumerateDirectoryPaths(String pattern = QuickIOPatterns.PathMatchAll, SearchOption searchOption = SearchOption.TopDirectoryOnly, QuickIOPathType pathFormatReturn = QuickIOPathType.Regular, QuickIOEnumerateOptions enumerateOptions = QuickIOEnumerateOptions.None)
        {
            Contract.Requires(!String.IsNullOrWhiteSpace(pattern));
            Contract.Ensures(Contract.Result <IEnumerable <string> >() != null);

            return(QuickIODirectory.EnumerateDirectoryPaths(FullNameUnc, pattern, searchOption, pathFormatReturn, enumerateOptions));
        }
        /// <summary>
        /// Returns an enumerable collection of files in a specified path.
        /// </summary>
        /// <param name="pattern">Search pattern. Uses Win32 native filtering.</param>
        /// <param name="searchOption">Specifiy depth with <see cref="SearchOption"/></param>
        /// <param name="enumerateOptions">Options <see cref="QuickIOEnumerateOptions"/></param>
        /// <returns>An enumerable collection of the full names (including paths) for the files in the directory specified by path.</returns>
        public IEnumerable <QuickIOFileInfo> EnumerateFiles(String pattern = QuickIOPatterns.PathMatchAll, SearchOption searchOption = SearchOption.TopDirectoryOnly, QuickIOEnumerateOptions enumerateOptions = QuickIOEnumerateOptions.None)
        {
            Contract.Requires(!String.IsNullOrWhiteSpace(pattern));
            Contract.Ensures(Contract.Result <IEnumerable <QuickIOFileInfo> >() != null);

            return(QuickIODirectory.EnumerateFiles(FullNameUnc, pattern, searchOption));
        }
        /// <summary>
        /// Compresses a directory by using <see>
        ///         <cref>ZipFile.CreateFromDirectory</cref>
        ///     </see>
        /// </summary>
        /// <param name="directoryFullPath">Directory fullname to zip</param>
        /// <param name="zipFullPath">Zipfile fullname to save</param>
        /// <param name="overWriteExistingZip">true to overwrite existing zipfile</param>
        /// <param name="compressionLevel"><see cref="CompressionLevel"/></param>
        /// <param name="includeBaseDirectory">True to include basedirectory</param>
        /// <exception cref="DirectoryNotFoundException">if <paramref name="directoryFullPath"/> does not exist.</exception>
        /// <exception cref="FileAlreadyExistsException">if <paramref name="zipFullPath"/> does exist and <paramref name="overWriteExistingZip"/> is <i>false</i>.</exception>
        public static void Compress(String directoryFullPath, String zipFullPath, bool overWriteExistingZip = false, CompressionLevel compressionLevel = CompressionLevel.Fastest, bool includeBaseDirectory = false)
        {
            Contract.Requires(!String.IsNullOrWhiteSpace(directoryFullPath));
            Contract.Requires(!String.IsNullOrWhiteSpace(zipFullPath));

            if (!QuickIODirectory.Exists(directoryFullPath))
            {
                throw new DirectoryNotFoundException($"Directory to zip '{directoryFullPath}' does not exist.");
            }

            if (!overWriteExistingZip && (QuickIOFile.Exists(zipFullPath)))
            {
                throw new FileAlreadyExistsException($"The target zipFile name '{zipFullPath}' already exists.");
            }

            throw new NotImplementedException();
            //ZipFile.CreateFromDirectory( directoryFullPath, zipFullPath, compressionLevel, includeBaseDirectory );
        }