Exemplo n.º 1
0
        public void TestExceptions()
        {
            var fs = new MemoryFileSystem();

            Assert.Throws <ArgumentNullException>(() => fs.AppendAllText("/a.txt", null));
            Assert.Throws <ArgumentNullException>(() => fs.WriteAllText("/a.txt", null));
            Assert.Throws <ArgumentNullException>(() => fs.WriteAllText("/a.txt", "content", null));
            Assert.Throws <ArgumentNullException>(() => fs.WriteAllText("/a.txt", null, null));
            Assert.Throws <ArgumentNullException>(() => fs.ReadAllText("/a.txt", null));
            Assert.Throws <ArgumentNullException>(() => fs.ReadAllLines("/a.txt", null));
            Assert.Throws <ArgumentNullException>(() => fs.WriteAllBytes("/a", null));
            Assert.Throws <ArgumentNullException>(() => fs.AppendAllText("/a", null, null));
            Assert.Throws <ArgumentNullException>(() => fs.AppendAllText("/a", "content", null));
            Assert.Throws <ArgumentNullException>(() => fs.EnumeratePaths("*", null).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumeratePaths("*", null, SearchOption.AllDirectories).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateFiles("*", null).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateFiles("*", null, SearchOption.AllDirectories).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateDirectories("*", null).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateDirectories("*", null, SearchOption.AllDirectories).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateFileEntries("*", null).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateFileEntries("*", null, SearchOption.AllDirectories).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateDirectoryEntries("*", null).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateDirectoryEntries("*", null, SearchOption.AllDirectories).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateFileSystemEntries("*", null).First());
            Assert.Throws <ArgumentNullException>(() => fs.EnumerateFileSystemEntries("*", null, SearchOption.AllDirectories).First());
            Assert.Throws <FileNotFoundException>(() => fs.GetFileEntry("/a.txt"));
            Assert.Throws <DirectoryNotFoundException>(() => fs.GetDirectoryEntry("/a"));
        }
Exemplo n.º 2
0
        public void 修改檔案日期()
        {
            var rootUPath = CreateRootPath();

            using var fileSystem = new MemoryFileSystem();

            var subName = "TestFolder";

            var subPath      = $"{rootUPath}/{subName}";
            var subPath1     = $"{subPath}/1";
            var subFile1     = $"{subPath}/1/1.txt";
            var subFile2     = $"{subPath}/1/2.txt";
            var subPath1_1   = $"{subPath}/1/1_1";
            var subFile1_1   = $"{subPath}/1/1_1/1_1.txt";
            var subPath1_1_1 = $"{subPath}/1/1_1/1_1_1";
            var subPath2     = $"{subPath}/2";
            var content      = "This is test string";
            var contentBytes = Encoding.UTF8.GetBytes(content);

            if (fileSystem.DirectoryExists(subPath1_1_1) == false)
            {
                fileSystem.CreateDirectory(subPath1_1_1);
            }

            if (fileSystem.DirectoryExists(subPath2) == false)
            {
                fileSystem.CreateDirectory(subPath2);
            }

            if (fileSystem.FileExists(subFile1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            if (fileSystem.FileExists(subFile1_1) == false)
            {
                using var stream =
                          fileSystem.OpenFile(subFile1_1, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            var fileEntry = fileSystem.GetFileEntry(subFile1);

            fileEntry.CreationTime   = new DateTime(1900, 1, 1);
            fileEntry.LastWriteTime  = new DateTime(1900, 1, 2);
            fileEntry.LastAccessTime = new DateTime(1900, 1, 3);

            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath1_1_1));
            Assert.AreEqual(true, fileSystem.DirectoryExists(subPath2));

            fileSystem.DeleteDirectory(subPath, true);
        }