Ejemplo n.º 1
0
 public static void EnsureDirectory(this IFileSystem fileSystem, Path directory)
 {
     var path = directory.ToString();
     if(fileSystem.DirectoryExists(path))
         return;
     fileSystem.CreateDirectory(path);
 }
Ejemplo n.º 2
0
 internal static void Copy(this IFileSystem fileSystem, string source, string destination)
 {
     // if copying a file
     if (File.Exists(source))
     {
         fileSystem.WriteFile(destination, File.ReadAllText(source));
         return;
     }
     
     // if copying a directory
     if (fileSystem.DirectoryExists(destination))
     {
         fileSystem.DeleteDirectory(destination);
     }
     fileSystem.CreateDirectory(destination);
 
     // Copy dirs recursively
     foreach (var child in Directory.GetDirectories(Path.GetFullPath(source), "*", SearchOption.TopDirectoryOnly).Select(p => Path.GetDirectoryName(p)))
     {
         fileSystem.Copy(Path.Combine(source, child), Path.Combine(destination, child));
     }
     // Copy files
     foreach (var childFile in Directory.GetFiles(Path.GetFullPath(source), "*", SearchOption.TopDirectoryOnly).Select(p=>Path.GetFileName(p)))
     {
         fileSystem.Copy(Path.Combine(source, childFile), 
                         Path.Combine(destination, childFile));
     }
    
 }
Ejemplo n.º 3
0
        public static void AppendToLogFile(this IFileSystem fileSystem, string filename, string contents)
        {
            lock (LogLock)
            {
                var logsDirectory = RippleLogsDirectory();
                fileSystem.CreateDirectory(logsDirectory);

                fileSystem.AppendStringToFile(logsDirectory.AppendPath(filename), contents);
            }
        }
 public static void CreateDirectoryRecursive(this IFileSystem fileSystem, FileSystemPath path)
 {
     if (!path.IsDirectory)
         throw new ArgumentException("The specified path is not a directory.");
     var currentDirectoryPath = FileSystemPath.Root;
     foreach(var dirName in path.GetDirectorySegments())
     {
         currentDirectoryPath = currentDirectoryPath.AppendDirectory(dirName);
         if (!fileSystem.Exists(currentDirectoryPath))
             fileSystem.CreateDirectory (currentDirectoryPath);
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Creates all the children of the specified directory in the specified path 
        /// </summary>        
        /// <param name="visitor"></param>
        /// <param name="directory">The directory to create</param>
        /// <param name="createIn">The filesystem path to create the directory in</param>
        /// <param name="deleteExisting">
        /// If set to true, and the specified path already exists,
        /// all files currently at that path will be deleted before the directory is created in the specified location
        /// </param>
        public static void CreateDirectoryInPlace(this LocalItemCreator visitor, IDirectory directory, string createIn, bool deleteExisting = false)
        {
            if (deleteExisting && NativeDirectory.Exists(createIn))
            {
                NativeDirectory.Delete(createIn, true);
            }

            var name = Path.GetFileName(createIn.Trim("\\//".ToCharArray()));
            createIn = Path.GetDirectoryName(createIn);

            visitor.CreateDirectory(new Directory(name, directory.Directories, directory.Files), createIn);            
        }
        public static void EnsurePath(this IsolatedStorageFile store, string filename)
        {
            for (string path = Path.GetDirectoryName(filename);
            path != "";
            path = Path.GetDirectoryName(path))
            {

                if (!store.DirectoryExists(path))
                {
                    store.CreateDirectory(path);
                }
            }

        }
        public static Task CopyDirectoryAsync(this IsolatedStorageFile Iso, string SourceDirectory, string TargetDirectory, IProgress<double> FractionProgress, bool OverWrite = false)
        {
            Contract.Requires(Iso != null);
            Contract.Requires(FractionProgress != null);
            Contract.Requires(Iso.DirectoryExists(SourceDirectory), "Source Directory does not exist");

            return Task.Factory.StartNew(() =>
            {
                FractionProgress.Report(0.0);

                IList<string> relativeFilePaths;
                IList<string> relativeDirPaths;
                CollectSubdirectoriesAndFilesBreadthFirst(Iso, SourceDirectory, out relativeDirPaths, out relativeFilePaths);

                var totalElementCount =
                    relativeDirPaths.Count + //SubDirectories
                    1 + //TargetDir
                    relativeFilePaths.Count; // Files

                var reporter = new PercentageReporter<double>(FractionProgress, p => p / 100.0, totalElementCount);

                var absoluteDirs = from relativeDir in relativeDirPaths
                                   select Path.Combine(TargetDirectory, relativeDir);

                foreach (var dir in Enumerable.Repeat(TargetDirectory, 1).Concat(absoluteDirs))
                {
                    if (!Iso.DirectoryExists(dir))
                    {
                        Iso.CreateDirectory(dir);
                    }
                    reporter.Completed++;
                }

                foreach (var relativeFile in relativeFilePaths)
                {
                    var sourceFile = Path.Combine(SourceDirectory, relativeFile);
                    var targetFile = Path.Combine(TargetDirectory, relativeFile);

                    try
                    {
                        Iso.CopyFile(sourceFile, targetFile, OverWrite);
                    }
                    catch (IsolatedStorageException)
                    {
                        // Ignore
                    }
                    reporter.Completed++;
                }
            });
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Gets a child directory and optionally creates it if missing
        /// </summary>
        /// <param name="root">The root directory</param>
        /// <param name="childName">The name of the direct child directory</param>
        /// <param name="createIfMissing">If <c>true</c>, the child directory will be created if does not exist</param>
        /// <exception cref="ArgumentException">Thrown if the child directory does not exist and <c>createIfMissing</c> parameter is false</exception>
        /// <returns>Returns the file system abstraction of the child directory</returns>
        public static IFileSystemDirectory GetChildDirectory(this IFileSystemDirectory root, string childName, bool createIfMissing)
        {
            Contract.Requires(root != null);
            Contract.Requires(!String.IsNullOrWhiteSpace(childName));
            Contract.Ensures(Contract.Result<IFileSystemDirectory>() != null);

            if (root.ChildDirectories.Any(name => name.Equals(childName, StringComparison.InvariantCultureIgnoreCase)))
            {
                return root.GetChildDirectory(childName);
            }
            else
            {
                if (createIfMissing)
                    return root.CreateDirectory(childName);
                else
                    throw new ArgumentException("The argument is not a child directory of this directory", "childName");
            }
        }
Ejemplo n.º 9
0
        public static void CopyFromDisk(this IDirectory directory, DirectoryInfo sourceDir)
        {
            if (directory == null)
            {
                throw new ArgumentNullException("directory");
            }

            if (sourceDir == null)
            {
                return;
            }

            foreach (var fileInfo in sourceDir.GetFiles())
            {
                directory.CreateFileFromDisk(fileInfo.Name, fileInfo);
            }

            foreach (var subDir in sourceDir.GetDirectories())
            {
                var dir = directory.CreateDirectory(subDir.Name) as MemoryDirectory;
                dir.CopyFromDisk(subDir);
            }
        }
Ejemplo n.º 10
0
        private static void UploadDirectoryOrFile(this FtpClient conn, string serverDirectory, FileSystemInfo fileSystem)
        {
            // Upload the file directly.
            if (fileSystem is FileInfo)
            {
                var fileUri = string.Format("{0}/{1}", serverDirectory, fileSystem.Name);
                Upload(conn, fileUri, fileSystem as FileInfo);
            }
            // Upload a directory.
            else
            {
                // Construct the sub directory Uri.
                var subDirectoryPath = string.Format("{0}/{1}", serverDirectory, fileSystem.Name);
                if (!conn.DirectoryExists(subDirectoryPath)) conn.CreateDirectory(subDirectoryPath);

                // Get the sub directories and files.
                var subDirectoriesAndFiles = (fileSystem as DirectoryInfo).GetFileSystemInfos();
                // Upload the files in the folder and sub directories.
                foreach (var subFile in subDirectoriesAndFiles)
                {
                    UploadDirectoryOrFile(conn, subDirectoryPath, subFile);
                }
            }
        }
Ejemplo n.º 11
0
        public static void TruncateLogFile(this IFileSystem fileSystem, string filename)
        {
            lock (LogLock)
            {
                var logsDirectory = RippleLogsDirectory();
                fileSystem.CreateDirectory(logsDirectory);

                fileSystem.AlterFlatFile(logsDirectory.AppendPath(filename), contents => contents.Clear());
            }
        }
Ejemplo n.º 12
0
        internal static void EnsureDirectory(this IDirectory directory, string filePath, out IDirectory dir, out string fileName)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            filePath = filePath.Trim('/');

            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentException("filePath");
            }

            var index = filePath.LastIndexOf('/');
            if (index < 0)
            {
                dir = directory;
                fileName = filePath;
            }
            else
            {
                dir = directory.CreateDirectory(filePath.Substring(0, index));
                fileName = filePath.Substring(index + 1);
            }
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Creates the specified directory structure as temporary directory
 /// </summary>
 /// <returns>Returns a <see cref="DisposableLocalDirectoryWrapper"/> for the created directory that will delete the directory when disposed</returns>
 public static DisposableLocalDirectoryWrapper CreateTemporaryDirectory(this LocalItemCreator visitor, IDirectory directory)
 {
     return visitor.CreateDirectory(directory, Path.GetTempPath()).ToTemporaryDirectory();
 }