Example #1
0
        public void TestSetCreationTime()
        {
            var filename = Util.CreateNewFile(longPathDirectory);

            try
            {
                DateTime dateTime = DateTime.Now.AddDays(1);
                File.SetCreationTime(filename, dateTime);
                var fi = new FileInfo(filename);
                Assert.AreEqual(fi.CreationTime, dateTime);
            }
            finally
            {
                File.Delete(filename);
            }
        }
Example #2
0
        public void TestCopyWithoutOverwriteAndExistingFile()
        {
            var destLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename (Copy).ext").ToString();

            File.Copy(longPathFilename, destLongPathFilename);

            try
            {
                Assert.IsTrue(File.Exists(destLongPathFilename));
                Assert.Throws <IOException>(() => File.Copy(longPathFilename, destLongPathFilename));
            }
            finally
            {
                File.Delete(destLongPathFilename);
            }
        }
Example #3
0
        public void TestWriteAllLinesWithEncodingEnumerable()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file26.ext").ToString();

            File.WriteAllLines(tempLongPathFilename, new List <string> {
                "file26"
            }, Encoding.Unicode);
            try
            {
                Assert.AreEqual("file26" + Environment.NewLine, File.ReadAllText(tempLongPathFilename, Encoding.Unicode));
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Example #4
0
        public void TestSetLastAccessTimeUtc()
        {
            var filename = Util.CreateNewFile(longPathDirectory);

            try
            {
                DateTime dateTime = DateTime.UtcNow.AddDays(1);
                var      fi       = new FileInfo(filename);
                fi.LastAccessTimeUtc = dateTime;
                Assert.AreEqual(dateTime, File.GetLastAccessTimeUtc(filename));
            }
            finally
            {
                File.Delete(filename);
            }
        }
Example #5
0
        public void TestOpenReadWithWrite()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file31.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);

            try
            {
                using (var fileStream = fi.Open(FileMode.Append, FileAccess.Read))
                {
                    fileStream.WriteByte(43);
                }
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Example #6
0
        public void TestReadAllBytes()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString();

            using (var fileStream = File.Create(tempLongPathFilename))
            {
                fileStream.WriteByte(42);
            }
            try
            {
                Assert.IsTrue(new byte[] { 42 }.SequenceEqual(File.ReadAllBytes(tempLongPathFilename)));
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Example #7
0
        public void TestCopyWithOverwrite()
        {
            var destLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename (Copy).ext").ToString();

            File.Copy(longPathFilename, destLongPathFilename);

            try
            {
                Assert.IsTrue(File.Exists(destLongPathFilename));
                File.Copy(longPathFilename, destLongPathFilename, true);
                Assert.AreEqual(File.ReadAllText(longPathFilename), File.ReadAllText(destLongPathFilename));
            }
            finally
            {
                File.Delete(destLongPathFilename);
            }
        }
Example #8
0
        public void TestCopyToWithoutOverwriteAndExistingFile()
        {
            var fi = new FileInfo(longPathFilename);
            var destLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename (Copy).ext").ToString();

            fi.CopyTo(destLongPathFilename);

            try
            {
                Assert.IsTrue(File.Exists(destLongPathFilename));
                fi.CopyTo(destLongPathFilename);
            }
            finally
            {
                File.Delete(destLongPathFilename);
            }
        }
Example #9
0
        public void TestReadAllTextEncoding()
        {
            var          filename = new StringBuilder(longPathDirectory).Append(@"\").Append("file3.ext").ToString();
            const string fileText = "test";

            using (File.CreateText(filename))
            {
            }
            try
            {
                File.WriteAllText(filename, fileText, Encoding.Unicode);
                Assert.AreEqual(fileText, File.ReadAllText(filename, Encoding.Unicode));
            }
            finally
            {
                File.Delete(filename);
            }
        }
Example #10
0
        public void TestSetLastAccessTime()
        {
            var filename = Util.CreateNewFile(uncDirectory);

            try
            {
                DateTime dateTime = DateTime.Now.AddDays(1);
                var      fi       = new FileInfo(filename)
                {
                    LastAccessTime = dateTime
                };
                Assert.AreEqual(dateTime, File.GetLastAccessTime(filename));
            }
            finally
            {
                File.Delete(filename);
            }
        }
Example #11
0
        public void TestCreate()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString();

            try
            {
                using (var s = File.Create(tempLongPathFilename))
                {
                    s.WriteByte(42);
                    s.Seek(0, SeekOrigin.Begin);
                    Assert.AreEqual(42, s.ReadByte());
                }
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Example #12
0
        public void TestSetCreationTimeUtc()
        {
            var filename = Util.CreateNewFile(uncDirectory);

            try
            {
                DateTime dateTime = DateTime.UtcNow.AddDays(1);
                var      fi       = new FileInfo(filename)
                {
                    CreationTimeUtc = dateTime
                };
                Assert.AreEqual(dateTime, File.GetCreationTimeUtc(filename));
            }
            finally
            {
                File.Delete(filename);
            }
        }
Example #13
0
        public void TestCopyToWithOverwrite()
        {
            var fi = new FileInfo(filePath);
            var destLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("filename (Copy).ext").ToString();

            fi.CopyTo(destLongPathFilename);

            try
            {
                Assert.IsTrue(File.Exists(destLongPathFilename));
                fi.CopyTo(destLongPathFilename, true);
                Assert.AreEqual(File.ReadAllText(filePath), File.ReadAllText(destLongPathFilename));
            }
            finally
            {
                File.Delete(destLongPathFilename);
            }
        }
Example #14
0
        public void TestAppendText()
        {
            string filename = Util.CreateNewFile(longPathDirectory);

            try
            {
                using (var sw = File.AppendText(filename))
                {
                    sw.WriteLine("end of file");
                }
                var lines = File.ReadLines(filename);
                Assert.IsTrue(new[] { "beginning of file", "end of file" }.SequenceEqual(lines));
            }
            finally
            {
                File.Delete(filename);
            }
        }
Example #15
0
        public void TestLengthWithBadPath()
        {
            var filename = Util.CreateNewFile(longPathDirectory);

            Pri.LongPath.FileInfo fi;
            try
            {
                fi = new FileInfo(filename);
            }
            catch
            {
                File.Delete(filename);
                throw;
            }
            fi.Delete();
            fi.Refresh();
            var l = fi.Length;
        }
        // clean all files and directories in iPath, then delete iPath dir...
        public static void DeleteDirectory(string iPath, string[] ignoreFiles = null)
        {
            if (iPath == null)
            {
                return;
            }
            if (Application.platform == RuntimePlatform.WindowsEditor)
            {
                iPath = iPath.Replace('/', '\\');
            }

            if (!Directory.Exists(iPath))
            {
                return;
            }

            // delete subDir
            string[] dirPaths = Directory.GetDirectories(iPath);
            for (int i = 0; i < dirPaths.Length; i++)
            {
                DeleteDirectory(dirPaths[i], ignoreFiles);
            }

            // delete files
            string[] filePaths = Directory.GetFiles(iPath);
            for (int i = 0; i < filePaths.Length; ++i)
            {
                string extension = Path.GetExtension(filePaths[i]);
                if (ignoreFiles != null && Array.IndexOf(ignoreFiles, extension) >= 0)
                {
                    continue;
                }
                if (filePaths[i] != null)
                {
                    File.Delete(filePaths[i]);
                }
            }

            // delete iPath dir...
            if (Directory.GetDirectories(iPath).Length == 0 && Directory.GetFiles(iPath).Length == 0)
            {
                Directory.Delete(iPath);
            }
        }
Example #17
0
        public void TestOpenWithAccess()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append(Path.GetRandomFileName()).ToString();

            try
            {
                using (var s = File.Create(tempLongPathFilename))
                {
                    s.WriteByte(42);
                }
                using (File.Open(tempLongPathFilename, FileMode.Open, FileAccess.Read))
                {
                }
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Example #18
0
        public void TestReadAllTextWithEncoding()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file26.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);

            try
            {
                using (var streamWriter = File.CreateText(tempLongPathFilename, Encoding.Unicode))
                {
                    streamWriter.WriteLine("file26");
                }

                Assert.AreEqual("file26" + Environment.NewLine, File.ReadAllText(fi.FullName, Encoding.Unicode));
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Example #19
0
        public void TestMove()
        {
            string sourceFilename = Util.CreateNewFile(longPathDirectory);
            string destFilename   = Path.Combine(longPathDirectory, Path.GetRandomFileName());

            File.Move(sourceFilename, destFilename);
            try
            {
                Assert.IsFalse(File.Exists(sourceFilename));
                Assert.IsTrue(File.Exists(destFilename));
                Assert.IsTrue(Util.VerifyContentsOfNewFile(destFilename));
            }
            finally
            {
                if (File.Exists(destFilename))
                {
                    File.Delete(destFilename);
                }
            }
        }
Example #20
0
        public void TestOpenReadWithWrite()
        {
            var tempLongPathFilename = new StringBuilder(uncDirectory).Append(@"\").Append("file31.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);

            try
            {
                Assert.Throws <NotSupportedException>(() =>
                {
                    using (var fileStream = fi.Open(FileMode.Append, FileAccess.Read))
                    {
                        fileStream.WriteByte(43);
                    }
                });
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Example #21
0
        public void TestOpenRead()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString();

            try
            {
                using (var s = File.Create(tempLongPathFilename))
                {
                    s.WriteByte(42);
                }
                using (var stream = File.OpenRead(tempLongPathFilename))
                {
                    Assert.AreEqual(42, stream.ReadByte());
                }
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Example #22
0
        public void TestEncrypt()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString();

            try
            {
                using (var s = File.Create(tempLongPathFilename, 200))
                {
                }
                var preAttrib = File.GetAttributes(tempLongPathFilename);
                Assert.AreEqual((FileAttributes)0, (preAttrib & FileAttributes.Encrypted));
                File.Encrypt(tempLongPathFilename);
                var postAttrib = File.GetAttributes(tempLongPathFilename);
                Assert.AreEqual(FileAttributes.Encrypted, (postAttrib & FileAttributes.Encrypted));
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Example #23
0
        public void TestOpenExisting()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString();

            try
            {
                using (var s = File.Create(tempLongPathFilename))
                {
                    s.WriteByte(42);
                }
                using (var stream = File.Open(tempLongPathFilename, FileMode.Open))
                {
                    Assert.IsNotNull(stream);
                }
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Example #24
0
 static void CopyFiles(string source, string dest, bool move, bool includeArchives = true)
 {
     if (move)
     {
         MarkFolderWritable(source);
     }
     MarkFolderWritable(dest);
     Console.WriteLine("{0} {1} => {2}", move ? "Moving" : "Copying", source, dest);
     source = source.TrimEnd('\\');
     dest   = dest.TrimEnd('\\');
     if (!Directory.Exists(dest))
     {
         Directory.CreateDirectory(dest);
     }
     Directory.EnumerateDirectories(source, "*", SearchOption.AllDirectories).Select(d => d.Replace(source, dest)).ForEach(path => Directory.CreateDirectory(path));
     foreach (var file in Directory.EnumerateFiles(source, "*", SearchOption.AllDirectories).Where(f => Path.GetExtension(f) != ".nfo" && !Regex.IsMatch(Path.GetFileName(f), "All.Collection.Upload|WareZ-Audio", RegexOptions.IgnoreCase)).ToArray())
     {
         if (Path.GetExtension(file) == ".sfv")
         {
             continue;
         }
         //if (!includeArchives && Regex.IsMatch(Path.GetExtension(file), @"\.(rar|r\d+|zip|iso)"))
         if (!includeArchives && Path.GetDirectoryName(file) == source && Regex.IsMatch(Path.GetExtension(file), @"\.(rar|r\d+|zip)"))
         {
             continue;
         }
         var newFile = file.Replace(source, dest);
         if (move)
         {
             if (File.Exists(newFile))
             {
                 File.Delete(newFile);
             }
             File.Move(file, newFile);
         }
         else
         {
             File.Copy(file, newFile, true);
         }
     }
 }
Example #25
0
        public void TestLastWriteTime()
        {
            var filename = Util.CreateNewFile(longPathDirectory);

            try
            {
                var dateTime = DateTime.Now.AddDays(1);
                {
                    var fiTemp = new FileInfo(filename)
                    {
                        LastWriteTime = dateTime
                    };
                }
                var fi = new FileInfo(filename);
                Assert.AreEqual(dateTime, fi.LastWriteTime);
            }
            finally
            {
                File.Delete(filename);
            }
        }
Example #26
0
        public void TestOpenWriteWritesCorrectly()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file31a.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);

            try
            {
                using (var fileStream = fi.OpenWrite())
                {
                    fileStream.WriteByte(42);
                }
                using (var fileStream = fi.OpenRead())
                {
                    Assert.AreEqual(42, fileStream.ReadByte());
                }
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Example #27
0
        public void TestReadAllBytesOnHugeFile()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString();

            using (var fileStream = File.Create(tempLongPathFilename))
            {
                fileStream.Seek(((long)int.MaxValue) + 1, SeekOrigin.Begin);
                fileStream.WriteByte(42);
            }
            Assert.Throws <IOException>(() =>
            {
                try
                {
                    Assert.IsTrue(new byte[] { 42 }.SequenceEqual(File.ReadAllBytes(tempLongPathFilename)));
                }
                finally
                {
                    File.Delete(tempLongPathFilename);
                }
            });
        }
Example #28
0
 public void TearDown()
 {
     try
     {
         if (File.Exists(longPathFilename))
         {
             File.Delete(longPathFilename);
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine("Exception {0} deleting \"longPathFilename\"", e.ToString());
         throw;
     }
     finally
     {
         if (Directory.Exists(longPathRoot))
         {
             Directory.Delete(longPathRoot, true);
         }
     }
 }
Example #29
0
        public void TestOpenCreatesEmpty()
        {
            var tempLongPathFilename = Path.Combine(longPathDirectory, Path.GetRandomFileName());

            try
            {
                using (var writer = File.CreateText(tempLongPathFilename))
                {
                    writer.WriteLine("test");
                }

                var fi = new FileInfo(tempLongPathFilename);
                using (var fileStream = fi.Open(FileMode.Append, FileAccess.Read, FileShare.Read))
                {
                    Assert.AreEqual(-1, fileStream.ReadByte());
                }
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }
Example #30
0
        public void TestOpenHidden()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("file25.ext").ToString();
            var fi = new FileInfo(tempLongPathFilename);

            using (fi.Create())
            {
            }
            try
            {
                File.SetAttributes(fi.FullName, File.GetAttributes(fi.FullName) | FileAttributes.Hidden);

                using (var fileStream = fi.Open(FileMode.Create))
                {
                    Assert.IsNotNull(fileStream);
                }
            }
            finally
            {
                File.Delete(tempLongPathFilename);
            }
        }