コード例 #1
0
        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();
                }
            }
        }
コード例 #2
0
        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);
        }