Click here to Skip to main content
15,884,810 members
Home / Discussions / C#
   

C#

 
QuestionPdf Converter in Shared Add-in Pin
Member 1059193119-Feb-14 4:01
Member 1059193119-Feb-14 4:01 
AnswerRe: Pdf Converter in Shared Add-in Pin
Eddy Vluggen19-Feb-14 8:15
professionalEddy Vluggen19-Feb-14 8:15 
QuestionBackslash issue Pin
Ashfaque Hussain19-Feb-14 1:41
Ashfaque Hussain19-Feb-14 1:41 
AnswerRe: Backslash issue Pin
BotCar19-Feb-14 2:13
BotCar19-Feb-14 2:13 
AnswerRe: Backslash issue Pin
Richard MacCutchan19-Feb-14 2:55
mveRichard MacCutchan19-Feb-14 2:55 
AnswerRe: Backslash issue Pin
Simon_Whale19-Feb-14 4:44
Simon_Whale19-Feb-14 4:44 
AnswerRe: Backslash issue Pin
OriginalGriff19-Feb-14 8:31
mveOriginalGriff19-Feb-14 8:31 
QuestionOutOfMemoryException Pin
computerpublic18-Feb-14 23:00
computerpublic18-Feb-14 23:00 
C#
/*
I HAVE A LARGE FILE WHICH I AM READING AND CONVERTING FROM BYTE FORM TO DECIMAL. I THEN CONVERT THE FILE BACK TO BYTE FORM AND WRITE IT TO ANOTHER FOLDER ON MY DESKTOP. I AM GETTING "OUT OF MEMORY EXCEPTION". IS THERE A WAY TO SPECIFY TO THE MEMORY TO MOVE THE DATA IN SMALLER CHUNKS?  WHY IS TRYING TO MOVE IT ALL AT ONCE ANYWAY, ISN'T THIS VERY INEFFICIENT? MY OUTPUT IS BELOW.

Total Bytes = 821903722 bytes
Unhandled Exception: OutOfMemoryException.
Press any key to continue . . .
*/
<pre>using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Applica
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo da = new DirectoryInfo("C:\\Folder");
            FileInfo[] Arr = da.GetFiles();
            if (Arr.Length == 0)
            {
                throw new InvalidOperationException("No files found.");
            }
            // No need to loop through the array just to get the last item:
            FileInfo ap = Arr[Arr.Length - 1];
            long Totbyte = ap.Length;
            string filePath = ap.FullName;
            Console.WriteLine("Total Bytes = {0} bytes", Totbyte);
            // GetTempFileName *creates* the file, so it always exists:
            string temPath = Path.GetTempFileName();
            byte[] data = File.ReadAllBytes(filePath);
            File.WriteAllBytes(temPath, data);
            // Convert the bytes to decimals:
            decimal[] arry = new decimal[Totbyte];
            for (int count = 0; count < data.Length; count++)
            {
                arry[count] = data[count];
            }
            // Convert the decimals back to bytes:
            byte[] data2 = new byte[Totbyte];
            for (int count = 0; count < arry.Length; count++)
            {
                data2[count] = (byte)arry[count];
            }
            // Just to prove they're the same:
            if (data2.Length != data.Length)
            {
                throw new InvalidOperationException("Wrong length!");
            }
            for (int index = 0; index < data.Length; index++)
            {
                if (data[index] != data2[index])
                {
                    throw new InvalidOperationException("Data has changed at index " + index);
                }
            }
            // Write the bytes back to the file:
            string filePath2 = Path.Combine("C:\\check", Path.GetFileName(filePath));
            File.WriteAllBytes(filePath2, data2);
            // To prove they're still the same:
            data = File.ReadAllBytes(temPath);
            data2 = File.ReadAllBytes(filePath);
            if (data2.Length != data.Length)
            {
                throw new InvalidOperationException("Wrong length!");
            }
            for (int index = 0; index < data.Length; index++)
            {
                if (data[index] != data2[index])
                {
                    throw new InvalidOperationException("Data has changed at index " + index);
                }
            }

        }
    }
}



AnswerRe: OutOfMemoryException Pin
Richard MacCutchan18-Feb-14 23:41
mveRichard MacCutchan18-Feb-14 23:41 
AnswerRe: OutOfMemoryException Pin
V.18-Feb-14 23:42
professionalV.18-Feb-14 23:42 
GeneralRe: OutOfMemoryException Pin
computerpublic18-Feb-14 23:59
computerpublic18-Feb-14 23:59 
GeneralRe: OutOfMemoryException Pin
V.19-Feb-14 0:03
professionalV.19-Feb-14 0:03 
GeneralRe: OutOfMemoryException Pin
computerpublic19-Feb-14 0:10
computerpublic19-Feb-14 0:10 
GeneralRe: OutOfMemoryException Pin
V.19-Feb-14 0:31
professionalV.19-Feb-14 0:31 
GeneralRe: OutOfMemoryException Pin
Eddy Vluggen19-Feb-14 0:31
professionalEddy Vluggen19-Feb-14 0:31 
QuestionRe: OutOfMemoryException Pin
Richard Deeming19-Feb-14 2:24
mveRichard Deeming19-Feb-14 2:24 
GeneralRe: OutOfMemoryException Pin
Richard MacCutchan19-Feb-14 2:47
mveRichard MacCutchan19-Feb-14 2:47 
GeneralRe: OutOfMemoryException Pin
Paulo Zemek19-Feb-14 8:43
mvaPaulo Zemek19-Feb-14 8:43 
QuestionHow to Change Wave from byte array 32bit/ 24bits 11khz to Byte array 16bit 8kz Pin
snakelecaps18-Feb-14 22:31
snakelecaps18-Feb-14 22:31 
SuggestionRe: How to Change Wave from byte array 32bit/ 24bits 11khz to Byte array 16bit 8kz Pin
Richard Deeming19-Feb-14 2:21
mveRichard Deeming19-Feb-14 2:21 
GeneralRe: How to Change Wave from byte array 32bit/ 24bits 11khz to Byte array 16bit 8kz Pin
snakelecaps19-Feb-14 2:38
snakelecaps19-Feb-14 2:38 
GeneralRe: How to Change Wave from byte array 32bit/ 24bits 11khz to Byte array 16bit 8kz Pin
Richard Deeming19-Feb-14 2:40
mveRichard Deeming19-Feb-14 2:40 
AnswerRe: How to Change Wave from byte array 32bit/ 24bits 11khz to Byte array 16bit 8kz Pin
Bernhard Hiller19-Feb-14 21:38
Bernhard Hiller19-Feb-14 21:38 
GeneralRe: How to Change Wave from byte array 32bit/ 24bits 11khz to Byte array 16bit 8kz Pin
snakelecaps20-Feb-14 5:28
snakelecaps20-Feb-14 5:28 
QuestionDNS Suffix Pin
Ramug1018-Feb-14 22:11
Ramug1018-Feb-14 22:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.