CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2012
    Posts
    20

    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);

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.

  3. #3
    Join Date
    Dec 2012
    Posts
    20

    Re: out of memory exception reading large files.

    Quote Originally Posted by Arjay View Post
    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);
    }

  4. #4
    Join Date
    Dec 2012
    Posts
    20

    Re: out of memory exception reading large files.

    pleae help me about it.reply me coders

  5. #5
    Join Date
    Mar 2013
    Posts
    6

    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.

  6. #6
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: out of memory exception reading large files.

    HABO's answer on StackOverflow looks good to me: http://stackoverflow.com/questions/9...arge-files-net
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured