示例#1
0
 public static void LogStraightToFile(string msg)
 {
     lock (_lock)
     {
         File.AppendAllText(LogFile, $"{(DateTime.Now - _startTime).TotalSeconds:0.##} - {msg}\r\n");
     }
 }
示例#2
0
 public static void LogStraightToFile(string msg)
 {
     if (LogFile == default)
     {
         return;
     }
     lock (_lock)
     {
         File.AppendAllText(LogFile.ToString(), $"{(DateTime.Now - _startTime).TotalSeconds:0.##} - {msg}\r\n");
     }
 }
示例#3
0
        public void File_AppendAllText()
        {
            Console.WriteLine("File.AppendAllText()");
            Console.WriteLine("\n\tDefault AlphaFS Encoding: [{0}]", NativeMethods.DefaultFileEncoding.EncodingName);

            // Create file and append text.
            var tempFile = Path.GetTempFileName();

            var allLines = UnitTestConstants.TextHelloWorld;

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

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

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

                Assert.IsTrue(line.Contains(allLines));
            }

            // Append
            File.AppendAllText(tempFile, "Append 1");
            File.AppendAllText(tempFile, allLines);
            File.AppendAllText(tempFile, "Append 2");
            File.AppendAllText(tempFile, allLines);

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

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

                Assert.IsTrue(line.Contains(allLines));
                Assert.IsTrue(line.Contains("Append 1"));
                Assert.IsTrue(line.Contains("Append 2"));
            }

            File.Delete(tempFile, true);
            Assert.IsFalse(File.Exists(tempFile), "Cleanup failed: File should have been removed.");
        }
示例#4
0
        public void File_Encrypt()
        {
            Console.WriteLine("File.Encrypt()");

            // Create file and append text.
            var tempFile = Path.GetTempFileName();

            // Append text as UTF-8, default.
            File.AppendAllText(tempFile, UnitTestConstants.TextHelloWorld);

            var utf8             = NativeMethods.DefaultFileEncoding.BodyName.ToUpperInvariant();
            var readText8        = File.ReadAllText(tempFile);
            var actual           = File.GetAttributes(tempFile);
            var encryptionStatus = File.GetEncryptionStatus(tempFile);

            Console.WriteLine("\n\tCreated {0} file: [{1}]", utf8, tempFile);
            Console.WriteLine("\tContent: [{0}]", readText8);
            Console.WriteLine("\n\tFile.GetAttributes(): [{0}]", actual);
            Console.WriteLine("\tEncryption status   : [{0}]", encryptionStatus);

            var encryptOk = false;

            try
            {
                File.Encrypt(tempFile);
                encryptOk = true;
                actual    = File.GetAttributes(tempFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\tCaught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            encryptionStatus = File.GetEncryptionStatus(tempFile);
            Console.WriteLine("\n\tFile.Encrypt() (Should be True): [{0}]", encryptOk);
            Console.WriteLine("\tFile.GetAttributes()           : [{0}]", actual);
            Console.WriteLine("\tEncryption status              : [{0}]", encryptionStatus);

            var decryptOk = false;

            try
            {
                File.Decrypt(tempFile);
                decryptOk = true;
                actual    = File.GetAttributes(tempFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("\n\tCaught (unexpected) {0}: [{1}]", ex.GetType().FullName, ex.Message.Replace(Environment.NewLine, "  "));
            }
            var decryptionStatus = File.GetEncryptionStatus(tempFile);

            Console.WriteLine("\n\tFile.Decrypt() (Should be True): [{0}]:", decryptOk);
            Console.WriteLine("\tFile.GetAttributes()           : [{0}]", actual);
            Console.WriteLine("\tDecryption status              : [{0}]", decryptionStatus);

            Assert.IsTrue(encryptOk, "File should be encrypted.");
            Assert.IsTrue(encryptionStatus == FileEncryptionStatus.Encrypted, "File should be encrypted.");
            Assert.IsTrue(decryptOk, "File should be decrypted.");
            Assert.IsTrue(decryptionStatus == FileEncryptionStatus.Encryptable, "File should be decrypted.");

            File.Delete(tempFile, true);
            Assert.IsFalse(File.Exists(tempFile), "Cleanup failed: File should have been removed.");
        }