示例#1
0
        public void 修改真實檔案日期()
        {
            var executingAssembly = Assembly.GetExecutingAssembly();
            var rootFolderPath    = Path.GetDirectoryName(executingAssembly.Location);
            var subFolder         = "TestFolder";
            var content           = "This is test string";

            if (Directory.Exists(subFolder) == false)
            {
                Directory.CreateDirectory(subFolder);
            }

            for (var i = 0; i < 5; i++)
            {
                var filePath = Path.Combine(rootFolderPath, subFolder, $"{i}.txt");

                var contentBytes = Encoding.UTF8.GetBytes($"{i}.{content}");
                File.WriteAllBytes(filePath, contentBytes);
            }

            //修改日期
            for (var i = 0; i < 5; i++)
            {
                var filePath = Path.Combine(rootFolderPath, subFolder, $"{i}.txt");
                File.SetCreationTime(filePath, new DateTime(2021, 1, 1));
                File.SetLastWriteTime(filePath, new DateTime(2021, 1, 1));
                File.SetLastAccessTime(filePath, new DateTime(2021, 1, 1));
            }

            //刪除檔案
            using var fileSystem = new Lexical.FileSystem.FileSystem(rootFolderPath);
            fileSystem.Delete(Path.Combine(subFolder), true);
        }
        private static void CreateTestFile1(string folderPath, string content)
        {
            var executingAssembly = Assembly.GetExecutingAssembly();
            var rootPath          = Path.GetDirectoryName(executingAssembly.Location);

            using (var folder = new Lexical.FileSystem.FileSystem(rootPath))
            {
                if (folder.Exists(folderPath) == false)
                {
                    folder.CreateDirectory(folderPath);
                }
            }

            using (var folder = new Lexical.FileSystem.FileSystem($"{rootPath}\\{folderPath}"))
            {
                for (var i = 0; i < 5; i++)
                {
                    var filePath     = $"{i}.txt";
                    var contentBytes = Encoding.UTF8.GetBytes($"{i}.{content}");

                    folder.CreateFile(filePath, contentBytes);
                }

                folder.PrintTo(Console.Out);
            }
        }
        private static Lexical.FileSystem.FileSystem CreateTestFile(string rootFolder, string subFolder, string content)
        {
            var fileSystem = new Lexical.FileSystem.FileSystem(rootFolder);

            if (fileSystem.Exists(subFolder) == false)
            {
                fileSystem.CreateDirectory(subFolder);
            }

            for (var i = 0; i < 5; i++)
            {
                var filePath     = Path.Combine(rootFolder, subFolder, $"{i}.txt");
                var contentBytes = Encoding.UTF8.GetBytes($"{i}.{content}");
                fileSystem.CreateFile(filePath, contentBytes);
            }

            var now = DateTime.UtcNow.AddDays(-30);

            for (var i = 0; i < 5; i++)
            {
                var filePath = Path.Combine(rootFolder, subFolder, $"{i}.txt");
                File.SetLastWriteTime(filePath, now);
                File.SetLastAccessTime(filePath, now);
                File.SetCreationTime(filePath, now);
            }

            return(fileSystem);
        }
        public void 列舉根路徑內的子資料夾()
        {
            var executingAssembly = Assembly.GetExecutingAssembly();
            var rootPath          = Path.GetDirectoryName(executingAssembly.Location);
            var subPath           = "TestFolder";
            var subPath1          = $"{subPath}/1";
            var subPath1_1        = $"{subPath}/1/1_1";
            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);

            Lexical.FileSystem.FileSystem fileSystem = null;
            try
            {
                fileSystem = new Lexical.FileSystem.FileSystem(rootPath);
                if (fileSystem.Exists(subPath1_1_1) == false)
                {
                    fileSystem.CreateDirectory(subPath1_1_1);
                }

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

                var file1 = $"{subPath1_1}/1_1.text";
                if (fileSystem.Exists(file1) == false)
                {
                    fileSystem.CreateFile(file1, contentBytes);
                }

                var file2 = Path.Combine(rootPath, subPath1_1_1, "1_1_1.txt");
                if (fileSystem.Exists(file2) == false)
                {
                    using var stream =
                              fileSystem.Open(file2, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                    stream.Write(contentBytes, 0, contentBytes.Length);
                }

                fileSystem.PrintTo(Console.Out, subPath);

                foreach (var entry in fileSystem.Browse(subPath))
                {
                    var path = entry.Path;
                    Console.WriteLine(path);
                }
            }
            finally
            {
                if (fileSystem != null)
                {
                    //還原
                    fileSystem.Delete(subPath, true);
                    fileSystem.Dispose();
                }
            }
        }
        public void 建立資料夾()
        {
            var executingAssembly = Assembly.GetExecutingAssembly();
            var rootPath          = Path.GetDirectoryName(executingAssembly.Location);
            var subPath           = "TestFolder";
            var subPath1          = $"{subPath}/1";
            var subPath1_1        = $"{subPath}/1/1_1";
            var subPath1_1_1      = $"{subPath}/1/1_1/1_1_1";
            var subPath2          = $"{subPath}/2";
            var content           = "This is test string";

            Lexical.FileSystem.FileSystem fileSystem = null;
            try
            {
                fileSystem = new Lexical.FileSystem.FileSystem(rootPath);
                if (fileSystem.Exists(subPath1_1_1) == false)
                {
                    fileSystem.CreateDirectory(subPath1_1_1);
                }

                if (fileSystem.Exists(subPath1_1) == false)
                {
                    fileSystem.CreateDirectory(subPath1_1);
                }

                if (fileSystem.Exists(subPath1) == false)
                {
                    fileSystem.CreateDirectory(subPath1);
                }

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

                fileSystem.PrintTo(Console.Out, subPath);
            }
            finally
            {
                if (fileSystem != null)
                {
                    //還原
                    fileSystem.Delete(subPath, true);
                    fileSystem.Dispose();
                }
            }
        }
        private static Lexical.FileSystem.FileSystem CreateFolder(string rootPath, string subPath)
        {
            var subPath1     = $"{subPath}/1";
            var subPath1_1   = $"{subPath}/1/1_1";
            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);

            var fileSystem = new Lexical.FileSystem.FileSystem(rootPath);

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

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

            var file1 = $"{subPath1_1}/1_1.text";

            if (fileSystem.Exists(file1) == false)
            {
                fileSystem.CreateFile(file1, contentBytes);
            }

            var file2 = Path.Combine(rootPath, subPath1_1_1, "1_1_1.txt");

            if (fileSystem.Exists(file2) == false)
            {
                using var stream =
                          fileSystem.Open(file2, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
                stream.Write(contentBytes, 0, contentBytes.Length);
            }

            return(fileSystem);
        }