I want to save the results of the following code in a text file.
private static readonly string cd = Directory.GetCurrentDirectory();
private static readonly StringBuilder sb = new StringBuilder();
public static void Main()
{
string listPath = cd + "/list.txt";
string rat = File.ReadAllText(listPath);
List<string> list = rat.Split(new char[] {'\r','\n'},StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string fl in list)
{
string rat2 = File.ReadAllText(fl);
List<string> file = rat2.Split(new char[] {'\r','\n'},StringSplitOptions.RemoveEmptyEntries).ToList();
foreach (string sn in file)
{
using (SHA256 mySHA256 = SHA256.Create())
{
byte[] hashValue = mySHA256.ComputeHash(Encoding.UTF8.GetBytes(sn));
sb.Clear();
foreach (Byte b in hashValue)
{
sb.Append(b.ToString("x2"));
}
string hashSHA256 = sb.ToString();
string res = $"{hashSHA256},{sn}{Environment.NewLine}";
string savePaths = $"{cd}/{hashSHA256.Substring(0,4)}.txt";
File.AppendAllText(savePaths, res);
}
}
}
Console.WriteLine("___END___");
}
list.txt Contains the following content
C:/file1.txt
C:/file2.txt
C:/file3.txt
The result of the file will be something like below
- 0000.txt
- 0001.txt
- 0002.txt
- ...
- fffd.txt
- fffe.txt
- ffff.txt
16^4 = 65,536 files
This increases the use of the hard drive and the execution speed is not optimal.
How can I increase the speed of execution and saving?
I know that saving in database might be a good option but we need to save in text file