Created
May 25, 2021 16:14
-
-
Save sebastienros/007d085fc1493d5ff4fea6b27b7c45fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Text.Json; | |
namespace testfs | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) | |
{ | |
for (var i=0; i<10; i++) | |
{ | |
CreateAllFiles(); | |
ReadAllFiles(); | |
DeleteAllFiles(); | |
ReadAllFiles(); | |
CreateAllFiles(); | |
ReadAllFiles(); | |
} | |
} | |
private static string Root = Path.GetFullPath("temp"); | |
private static string[] Paths = new string[] { | |
"firstFile.txt", | |
"FirstLevelDirectory\\secondFile.txt", | |
"FirstLevelDirectory\\SecondLevelDirectory\\thirdFile.txt", | |
"FirstLevelDirectory\\SecondLevelDirectory\\ThirdLevelDirectory\\fourthFile.txt", | |
"FirstLevelDirectory\\SecondLevelDirectory\\ThirdLevelDirectory\\1611678346913_20_INDIA - 6C - Login page - NETWORKING LOUNGE (f5e6955f-d106-4ebb-b46a-a83e9cd0845d).txt", | |
}; | |
public static void DeleteAllFiles() | |
{ | |
Console.WriteLine("Deleting files"); | |
var errors = new Dictionary<string, string>(); | |
foreach (var info in new DirectoryInfo(Root).GetFileSystemInfos()) | |
{ | |
if (info is FileInfo file) | |
{ | |
try | |
{ | |
File.Delete(file.FullName); | |
} | |
catch (Exception e) | |
{ | |
errors.Add(info.FullName, e.Message); | |
} | |
} | |
else if (info is DirectoryInfo dir) | |
{ | |
try | |
{ | |
Directory.Delete(dir.FullName, true); | |
} | |
catch (Exception e) | |
{ | |
errors.Add(info.FullName, e.Message); | |
} | |
} | |
} | |
if (errors.Count > 0) | |
{ | |
Console.WriteLine("Errors:"); | |
WriteContents(errors); | |
} | |
} | |
public static void CreateAllFiles() | |
{ | |
Console.WriteLine("Creating files"); | |
var errors = new Dictionary<string, string>(); | |
foreach (var path in Paths) | |
{ | |
var filename = Path.Combine(Root, path); | |
var dirPath = Path.GetDirectoryName(filename); | |
if (!Directory.Exists(dirPath)) | |
{ | |
Directory.CreateDirectory(dirPath); | |
} | |
try | |
{ | |
using var fs = File.CreateText(filename); | |
fs.WriteLine("hello world"); | |
} | |
catch (Exception e) | |
{ | |
errors.Add(path, e.Message); | |
} | |
} | |
if (errors.Count > 0) | |
{ | |
Console.WriteLine("Errors:"); | |
WriteContents(errors); | |
} | |
} | |
public static void ReadAllFiles() | |
{ | |
Console.WriteLine("Reading files"); | |
var contents = new Dictionary<string, string>(); | |
foreach (var path in Paths) | |
{ | |
var filename = Path.Combine(Root, path); | |
try | |
{ | |
if (File.Exists(filename)) | |
{ | |
contents.Add(path, File.ReadAllText(filename)); | |
} | |
} | |
catch (Exception e) | |
{ | |
contents.Add(path, e.Message); | |
} | |
} | |
Console.WriteLine("Results:"); | |
WriteContents(contents); | |
} | |
public static void WriteContents(object content) | |
{ | |
Console.WriteLine(JsonSerializer.Serialize(content, new JsonSerializerOptions { WriteIndented = true })); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment