为什么我的C#-console应用程序因复制文件时“内存不足”异常而崩溃?

为什么我的C#-console应用程序因复制文件时“内存不足”异常而崩溃?,第1张

概述我做了一个非常简单的程序,可以在外部HDD上找到图片,并将它们放入文件夹中. 这听起来很简单,但出于某种原因,我在这样做的时候得到了“Out of Memory”异常. 我已经在64位Win10上测试了4 GB的内存,64位的Win10和32 GB的内存.然而,我仍然在两个系统上都获得了“Out of Memory”特例. 我的平台目标是x64. 这是发生错误的代码: string[] fileP 我做了一个非常简单的程序,可以在外部HDD上找到图片,并将它们放入文件夹中.
这听起来很简单,但出于某种原因,我在这样做的时候得到了“Out of Memory”异常.

我已经在64位Win10上测试了4 GB的内存,64位的Win10和32 GB的内存.然而,我仍然在两个系统上都获得了“Out of Memory”特例.

我的平台目标是x64.

这是发生错误的代码:

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

所以我的问题:导致异常的原因是什么,我该如何解决?

解决方法 如其他答案中所述,您可以使用 using statement以正确的方式在对象上调用dispose方法.

using (Image Billede = Bitmap.Fromfile(file)){     ... }

但是,对于您的代码,您可能需要检查图像格式,因为如果(来自MSDN),Image.Fromfile函数将抛出OutOfMemoryException:

The file does not have a valID image format.

-or-

GDI+ does not support the pixel format of the file.

看看this

总结

以上是内存溢出为你收集整理的为什么我的C#-console应用程序复制文件时“内存不足”异常而崩溃?全部内容,希望文章能够帮你解决为什么我的C#-console应用程序因复制文件时“内存不足”异常而崩溃?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1221084.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存