Exemplo n.º 1
0
        public void append()
        {
            var fileInfo = new FileInfo(Path.GetTempFileName());

            Assert.Throws <ArgumentNullException>(() => FileSystemExtensions.Append(null, Enumerable.Empty <byte>().ToArray()));
            Assert.Throws <ArgumentNullException>(() => fileInfo.Append((byte[])null));
            Assert.Throws <ArgumentNullException>(() => FileSystemExtensions.Append(null, string.Empty));
            Assert.Throws <ArgumentNullException>(() => fileInfo.Append((string)null));
            Assert.Throws <ArgumentNullException>(() => FileSystemExtensions.Append(null, Stream.Null));
            Assert.Throws <ArgumentNullException>(() => fileInfo.Append((Stream)null));

            var bytes = Guid.NewGuid().ToByteArray();

            WithFile(file =>
            {
                Assert.True(ReferenceEquals(file.Append(Enumerable.Empty <byte>().ToArray()), file));
                Assert.False(file.Exists);
            });
            WithFile(file =>
            {
                Assert.True(ReferenceEquals(file.Append(bytes), file));
                Assert.True(file.Exists);
                Assert.True(file.Bytes().SequenceEqual(bytes));
            });

            var text = Guid.NewGuid().ToString();

            WithFile(file =>
            {
                Assert.True(ReferenceEquals(file.Append(string.Empty), file));
                Assert.False(file.Exists);
            });
            WithFile(file =>
            {
                Assert.True(ReferenceEquals(file.Append(text), file));
                Assert.True(file.Exists);
                Assert.Equal(text, file.Text());
            });
            WithFile(file =>
            {
                Assert.True(ReferenceEquals(file.Append(text, Encoding.Unicode), file));
                Assert.True(file.Exists);
                Assert.Equal(text, file.Text(Encoding.Unicode));
            });

            WithFile(file =>
            {
                Assert.True(ReferenceEquals(file.Append(Stream.Null), file));
                Assert.True(file.Exists);
                Assert.Equal(0, file.Length);
            });
            WithFile(file => new MemoryStream(bytes).Do(stream =>
            {
                Assert.True(ReferenceEquals(file.Append(stream), file));
                Assert.True(file.Bytes().SequenceEqual(bytes));
            }));
        }
        public FileInfo[] GetTempFileInfo(string path)
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(path);

            DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
            FileInfo[]      files          = new FileInfo[1000];
            foreach (var file in directoryInfo.GetFiles())
            {
                files.Append(file);
            }
            for (int i = 0; i < directoryInfos.Length; i++)
            {
                FileInfo[] fileInfos = directoryInfos[i].GetFiles();
                foreach (var fileinfo in fileInfos)
                {
                    files.Append(fileinfo);
                }
            }
            return(files);
        }
 /// <summary>
 /// Appends a string to a file
 /// </summary>
 /// <param name="File">File to append to</param>
 /// <param name="Content">Content to save to the file</param>
 /// <param name="EncodingUsing">The type of encoding the string is using (defaults to ASCII)</param>
 /// <returns>The FileInfo object</returns>
 public static FileInfo Append(this FileInfo File, string Content, Encoding EncodingUsing = null)
 {
     if (File == null)
     {
         throw new ArgumentNullException("File");
     }
     if (EncodingUsing == null)
     {
         EncodingUsing = new ASCIIEncoding();
     }
     byte[] ContentBytes = EncodingUsing.GetBytes(Content);
     return(File.Append(ContentBytes));
 }