out of memory exception reading large files.
I am encrypting and storing 2 files in 1 and extracting it. Application like iExpress.or winzip or win rar.My code works but for big files it gives error out of memory exception
encrypting and storing section =
byte[] buffer = Properties.Resources.stub;
string sp = "[SPLITTER]";
File.WriteAllBytes(sfd.FileName, buffer);
byte[] file1 = Secure(File.ReadAllBytes(textBox1.Text));
byte[] file2 = Secure(File.ReadAllBytes(textBox2.Text));
File.AppendAllText(sfd.FileName, sp + Convert.ToBase64String(file1) + sp + F1 + sp + Convert.ToBase64String(file2) + sp + F2);
and here extracting and decrypting
string source = File.ReadAllText(Application.ExecutablePath);
string[] stringSeparators = new string[] { "[SPLITTER]" };
string[] Drop = source.Split(stringSeparators, StringSplitOptions.None);
byte[] file1 = Unsecure(Convert.FromBase64String(Drop[1]));
byte[] file2 = Unsecure(Convert.FromBase64String(Drop[3]));
File.WriteAllBytes(TempDir + Drop[2], file1);
File.WriteAllBytes(TempDir + Drop[4], file2);
Re: out of memory exception reading large files.
What does the secure function do?
A couple of tips...
Rather than using raw arrays, try using collections. Next try reading the files line by line (and clean up the memory or reuse the memory for each line).
Use IDisposable and a using block so that memory gets freed when you leave the scoping block.
Re: out of memory exception reading large files.
Quote:
Originally Posted by
Arjay
What does the secure function do?
A couple of tips...
Rather than using raw arrays, try using collections. Next try reading the files line by line (and clean up the memory or reuse the memory for each line).
Use IDisposable and a using block so that memory gets freed when you leave the scoping block.
secure function encrypting the bytes (data/files).
yes good sujection about collection or small chunks but dont know who to loop it or .... can you give me short code for that
and yes here is my secure function
public byte[] Secure(byte[] Data)
{
using (RijndaelManaged SA = new RijndaelManaged())
{
SA.IV = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7 };
SA.Key = new byte[] { 7, 6, 5, 4, 3, 2, 1, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
return SA.CreateEncryptor().TransformFinalBlock(Data, 0, Data.Length);
}
Re: out of memory exception reading large files.
pleae help me about it.reply me coders
Re: out of memory exception reading large files.
You already got some tips from arjay. Don't use File.ReadAllBytes() read the file in chunks or line by line instead.
Re: out of memory exception reading large files.