So, this is a part of a larger program but this is the code that is failing. Sometimes when I run the program everything works as intended, but sometimes for no apparent reason with the exact same file it throws the Index out of bound of array exception here:
string text1 = text.Substring(127761 * count, 122761);
The purpose of this block of code is to divide a large text file into smaller text file so the method _encrypt
doesn't throw an error.
string text = File.ReadAllText(file);
if (text.Length > 127761)
{
List<string> texts = new List<string>();
int count = 1;
for (int i = 0; i < text.Length; i++)
{
if (i == 127761 * count)
{
if (text.Length - 127761 * count >= 0)
{
string text1 = text.Substring(127761 * count, 122761);
texts.Add(text1);
count++;
}
else texts.Add(text.Substring(127761 * count));
}
}
for (int i = 0; i < texts.Count; i++)
{
File.WriteAllText(desktop_path + @"\encrypt" + (i + 1) + ".txt", texts[i]);
_encrypt(desktop_path + @"\encrypt" + (i + 1) + ".txt", where_to_place + @"\part" + (i + 1));
}
If anyone can help me I would be grateful