Exemple #1
0
        /// <summary>
        /// Gets a copy of a file.
        /// If the file doesn't exist, the copied file will not exist neither.
        /// </summary>
        /// <param name="fileSystem">The file or directory to be copied</param>
        /// <param name="path">The relative path (starting from the location of the original file) to the directory where the file will be copied to</param>
        /// <returns>The copied file</returns>
        private static FileSystemInfo CopyFile(FileSystemInfo fileSystem, string path)
        {
            if ((path != null) && (path.Trim() != ""))
            {
                if (fileSystem is FileInfo)
                {
                    FileInfo file       = (FileInfo)fileSystem;
                    FileInfo targetFile = FileSupport.ExpandRelativePath(file.Directory, path + "\\" + file.Name);
                    if (File.Exists(file.FullName))
                    {
                        if (!targetFile.Directory.Exists)
                        {
                            targetFile.Directory.Create();
                        }
                        file.CopyTo(targetFile.FullName, true);
                    }
                    else
                    {
                        if (File.Exists(targetFile.FullName))
                        {
                            targetFile.Delete();
                        }
                    }

                    return(new FileInfo(targetFile.FullName));
                }

                if (fileSystem is DirectoryInfo)
                {
                    DirectoryInfo dir       = (DirectoryInfo)fileSystem;
                    DirectoryInfo targetDir = FileSupport.ExpandRelativeDirectory(dir, path);

                    if (Directory.Exists(dir.FullName))
                    {
                        DirCopy(dir.FullName, targetDir.FullName, false);
                    }
                    else
                    {
                        if (Directory.Exists(targetDir.FullName))
                        {
                            Directory.Delete(targetDir.FullName);
                        }
                    }

                    return(targetDir);
                }

                return(null);
            }
            else
            {
                return(fileSystem);
            }
        }
Exemple #2
0
        /// <summary>
        /// Gets the relative path from a starting directory to a file
        /// </summary>
        /// <param name="baseDirectory">Starting point</param>
        /// <param name="targetFile">File to refer to</param>
        /// <returns></returns>
        public static string GetRelativePath(DirectoryInfo baseDirectory, FileInfo targetFile)
        {
            // When roots are different, the result is the full path of the target.
            if (!targetFile.Directory.Root.ToString().Equals(baseDirectory.Root.ToString()))
            {
                return(targetFile.FullName);
            }

            // Quick result when the target resides in the base directory.
            if (targetFile.Directory.FullName.Equals(baseDirectory.FullName))
            {
                return(targetFile.Name);
            }

            ArrayList baseDir = FileSupport.GetDirectoryArray(baseDirectory);
            ArrayList target  = FileSupport.GetDirectoryArray(targetFile.Directory);

            // Finally add the target file name
            return(FileSupport.GetRelativeDir(baseDir, target) + targetFile.Name);
        }