Пример #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());
         }
 }
Пример #2
0
 public static string GetTextContent(this FakeFile file, Encoding encoding)
 {
     if (file == null)
     {
         throw new ArgumentNullException(nameof(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, encoding))
         {
             return(reader.ReadToEnd());
         }
 }
Пример #3
0
 public static byte[] GetBinaryContent(this FakeFile file)
 {
     if (file == null)
     {
         throw new ArgumentNullException(nameof(file));
     }
     if (!file.Exists)
     {
         throw new FileNotFoundException("File could not be found.", file.Path.FullPath);
     }
     using (var stream = file.OpenRead())
         using (var reader = new BinaryReader(stream))
             using (var memory = new MemoryStream())
             {
                 reader.BaseStream.CopyTo(memory);
                 return(memory.ToArray());
             }
 }
Пример #4
0
        public void CopyFile(FakeFile file, FilePath destination, bool overwrite)
        {
            if (!file.Exists)
            {
                throw new FileNotFoundException("File does 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(CultureInfo.InvariantCulture, 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} does 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);
                }
        }