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

标签 c#-4.0 ioexception system.io.fileinfo

所以我正在编写一个快速应用程序,根据纵横比将我的壁纸整齐地分类到文件夹中。一切都很顺利,直到我尝试实际移动文件(使用 FileInfo.MoveTo())。应用程序抛出异常:

System.IO.IO异常
该进程无法访问该文件,因为它正被另一个进程使用。

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

这是一些相关的代码,请原谅糟糕的 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 handler
private 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 对象中打开的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4714210/

相关文章:

c# - 如何在域中以模拟用户身份运行提升的命令

.net - 如何从类属性中访问 web.config 值?

c# - .NET 4 中的自定义属性更改

c# - System.IO.File.Copy 抛出 System.UnauthorizedAccessException

c# 将文件从源文件夹复制到目标文件夹

asp.net-mvc - 在 Lucene.Net.Store.SimpleFSDirectory@ 中找不到段* 文件

java - 同时捕获java异常FileNotFound和IOException

java - 什么会在 Java 中引发 IOException?

java - IOException:服务器返回 400 错误代码

C# 在不使用 System.IO.FileInfo 的情况下获取文件属性