Ejemplo n.º 1
0
 /// <summary>
 /// Gets the text content of the file (UTF-8 encoding).
 /// </summary>
 /// <param name="file">The file.</param>
 /// <returns>The text content of the file.</returns>
 public static string GetTextContent(this FakeFile file)
 {
     if (!file.Exists)
     {
         throw new FileNotFoundException("File could not be found.", file.Path.FullPath);
     }
     using (var stream = file.OpenRead())
         using (var reader = new StreamReader(stream))
         {
             return(reader.ReadToEnd());
         }
 }
Ejemplo n.º 2
0
        public void CopyFile(FakeFile file, FilePath destination, bool overwrite)
        {
            if (!file.Exists)
            {
                throw new FileNotFoundException("File do not exist.");
            }

            // Already exists?
            var destinationFile = FindFile(destination);

            if (destinationFile != null)
            {
                if (!overwrite)
                {
                    const string format  = "{0} exists and overwrite is false.";
                    var          message = string.Format(format, destination.FullPath);
                    throw new IOException(message);
                }
            }

            // Directory exists?
            var directory = FindDirectory(destination.GetDirectory());

            if (directory == null || !directory.Exists)
            {
                throw new DirectoryNotFoundException("The destination path {0} do not exist.");
            }

            // Make sure the file exist.
            if (destinationFile == null)
            {
                destinationFile = new FakeFile(this, destination);
            }

            // Copy the data from the original file to the destination.
            using (var input = file.OpenRead())
                using (var output = destinationFile.OpenWrite())
                {
                    input.CopyTo(output);
                }
        }