I made a very simple program that could find pictures on an external-HDD, and place them into folders. That sounds very simple, but for some reason, I get a 'Out of Memory'-exception when doing so.
I've tested it on a 64-Bit Win10 with 4 GB of ram, and a 64-Bit Win10 with 32 GB of ram. Yet, I still get the 'Out of Memory'-exception on both systems.
My Platform-target is x64.
Here's the code where the error-occurs in:
string[] filePaths = Directory.GetFiles(Stien, "*.*", SearchOption.AllDirectories);
foreach (string file in filePaths)
{
string[] TempValue1 = file.Split(new[] { @"\" }, StringSplitOptions.None);
string FileName = TempValue1[TempValue1.Length - 1];
if (FileName.Contains(SøgeTerm)) //Checks if the name contains the search-term.
{
if (!SortDate) //If the program was told not to sort by date.
{
try
{
File.Copy(file, destination + @"\" + FileName, true);
Console.WriteLine(FileName + " => " + destination + @"\" + FileName);
}
catch (Exception e)
{
Console.WriteLine("Fejl: " + e.ToString());
}
}
else
{
Image Billede = Bitmap.FromFile(file);
string date;
try
{
PropertyItem propItem = Billede.GetPropertyItem(36867);
date = r.Replace(Encoding.UTF8.GetString(propItem.Value), "-", 2);
date = date.Split(new[] { ' ' }, StringSplitOptions.None)[0];
propItem = null;
}
catch
{
date = "UKENDT";
}
//
if (!Directory.Exists(destination + @"\" + date))
{
Directory.CreateDirectory(destination + @"\" + date);
Console.WriteLine(destination + @"\" + date);
}
File.Copy(file, destination + @"\" + date + @"\" + FileName, true); //Copies the file, and places it into the fitting folder.
Console.WriteLine(FileName + " => " + destination + @"\" + "" + date + @"\" + FileName);
date = null; //I thought this might helped clearing some memory.
Billede = null;
}
}
}
So my problem: What causes the exception, and how can I fix it?