示例#1
0
        public void File_AppendAllLinesThenReadAllLinesShouldReturnSameCollection()
        {
            var file   = Path.GetTempFileName();
            var sample = new [] { "line one", "line two" };

            try
            {
                File.AppendAllLines(file, sample);
                CollectionAssert.AreEquivalent(sample, File.ReadAllLines(file).ToArray());
            }
            finally
            {
                File.Delete(file);
            }
        }
示例#2
0
        private void DumpAppendAllLines(bool isLocal)
        {
            #region Setup

            Console.WriteLine("\n=== TEST {0} ===", isLocal ? UnitTestConstants.Local : UnitTestConstants.Network);
            var tempFolder = Path.GetTempPath();
            var tempPath   = Path.Combine(tempFolder, "File.Delete-" + Path.GetRandomFileName());
            if (!isLocal)
            {
                tempPath = Path.LocalToUnc(tempPath);
            }

            // Create file and append text.
            var tempFile = Path.GetTempFileName();
            if (!isLocal)
            {
                tempFile = Path.LocalToUnc(tempFile);
            }

            IEnumerable <string> allLines = new[] { UnitTestConstants.TenNumbers, UnitTestConstants.TextHelloWorld, UnitTestConstants.TextGoodbyeWorld, UnitTestConstants.TextUnicode };

            #endregion // Setup

            try
            {
                #region AppendAllLines

                Console.WriteLine("\nDefault AlphaFS Encoding: [{0}]", NativeMethods.DefaultFileEncoding.EncodingName);

                // Create real UTF-8 file.
                File.AppendAllLines(tempFile, allLines, NativeMethods.DefaultFileEncoding);

                // Read filestream contents.
                using (var streamRead = File.OpenText(tempFile))
                {
                    var line = streamRead.ReadToEnd();

                    Console.WriteLine("\nCreated: [{0}] filestream: [{1}]\n\n\tAppendAllLines content:\n{2}", streamRead.CurrentEncoding.EncodingName, tempFile, line);

                    foreach (var line2 in allLines)
                    {
                        Assert.IsTrue(line.Contains(line2));
                    }
                }

                // Append
                File.AppendAllLines(tempFile, new[] { "Append 1" });
                File.AppendAllLines(tempFile, allLines);
                File.AppendAllLines(tempFile, new[] { "Append 2" });
                File.AppendAllLines(tempFile, allLines);

                // Read filestream contents.
                using (var streamRead = File.OpenText(tempFile))
                {
                    var line = streamRead.ReadToEnd();

                    Console.WriteLine("AppendAllLines content:\n{0}", line);

                    foreach (var line2 in allLines)
                    {
                        Assert.IsTrue(line.Contains(line2));
                    }
                }

                #endregion // AppendAllLines
            }
            finally
            {
                File.Delete(tempFile, true);
                Assert.IsFalse(File.Exists(tempFile), "Cleanup failed: File should have been removed.");
            }

            Console.WriteLine();
        }