c#-4.0 – 如何告诉我的C#应用​​程序关闭它在FileInfo对象或可能的Bitmap对象中打开的文件?

c#-4.0 – 如何告诉我的C#应用​​程序关闭它在FileInfo对象或可能的Bitmap对象中打开的文件?,第1张

概述所以我正在写一个快速的应用程序,根据宽高比整齐地将我的壁纸排序到文件夹中.一切顺利,直到我尝试实际移动文件(使用FileInfo.MoveTo()).该应用程序抛出异常: System.IO.IOException 该进程无法访问该文件,因为该文件正由另一个进程使用. 唯一的问题是,我的计算机上没有其他进程正在打开该特定文件.我想也许是因为我使用该文件的方式,也许某个内部系统子程序在不同的线程上或 所以我正在写一个快速的应用程序,根据宽高比整齐地将我的壁纸排序到文件夹中.一切顺利,直到我尝试实际移动文件(使用fileInfo.Moveto()).该应用程序抛出异常:

System.IO.IOException
该进程无法访问该文件,因为该文件正由另一个进程使用.

唯一的问题是,我的计算机上没有其他进程正在打开该特定文件.我想也许是因为我使用该文件的方式,也许某个内部系统子程序在不同的线程上或某些东西在我尝试移动它时打开了文件.果然,在上面几行,我设置了一个属性,调用一个打开文件进行读取的事件.我假设至少有一些是异步发生的.反正它是否同步运行?我必须更改该属性或重写大部分代码.

这里有一些相关的代码,请原谅蹩脚的Visual C#默认名称,这不是真正的发布质量软件:

private voID button1_Click(object sender,EventArgs e){    for (uint i = 0; i < fileBox.Items.Count; i++)    {        if (!fileBox.GetItemChecked((int)i)) continue;        //This calls the selectedindexChanged event to change the 'selectedimg' variable        fileBox.Selectedindex = (int)i;        if (selectedimg == null) continue;        Size imgAspect = getimgAspect(selectedimg);        //This is gonna be hella hardcoded for Now        //In the future this should be changed to be generic        //and use some kind of setting schema to determine        //the sort/filter results        fileInfo file = ((fileInfo)fileBox.SelectedItem);        if (imgAspect.WIDth == 8 && imgAspect.Height == 5)        {            finalOut = outPath + "\8x5\" + file.name;        }        else if (imgAspect.WIDth == 5 && imgAspect.Height == 4)        {            finalOut = outPath + "\5x4\" + file.name;        }        else        {            finalOut = outPath + "\Other\" + file.name;        }        //Me trying to tell C# to close the file        selectedimg.dispose();        prevIEwer.Image = null;        //This is where the exception is thrown        file.Moveto(finalOut);    }}//The SUSPECTed event handlerprivate voID fileBox_SelectedindexChanged(object sender,EventArgs e){    fileInfo selected;    if (fileBox.Selectedindex >= fileBox.Items.Count || fileBox.Selectedindex < 0) return;    selected = (fileInfo)fileBox.Items[fileBox.Selectedindex];    try    {        //The SUSPECTed line of code        selectedimg = new Bitmap((Stream)selected.OpenRead());    }    catch (Exception) { selectedimg = null;  }    if (selectedimg != null)        prevIEwer.Image = ResizeImage(selectedimg,prevIEwer.Size);    else        prevIEwer.Image = null;}

我有一个长期的修复(无论如何这可能更有效)但它仍然存在更多问题:/

任何帮助将不胜感激.

解决方法 由于您使用selectedimg作为类范围变量,因此在打开位图时会对文件保持锁定.我会使用 using statement然后 Clone将Bitmap放入您正在使用的变量中,这将释放Bitmap保留在文件上的锁定.

像这样的东西.

using ( Bitmap img  = new Bitmap((Stream)selected.OpenRead())){    selectedimg = (Bitmap)img.Clone();}
总结

以上是内存溢出为你收集整理的c#-4.0 – 如何告诉我的C#应用​​程序关闭它在FileInfo对象或可能的Bitmap对象中打开的文件?全部内容,希望文章能够帮你解决c#-4.0 – 如何告诉我的C#应用​​程序关闭它在FileInfo对象或可能的Bitmap对象中打开的文件?所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/langs/1227768.html

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

发表评论

登录后才能评论

评论列表(0条)

保存