在DataGridViewRow中使用时无法删除图像 [英] Cannot delete image when used in DataGridViewRow

查看:105
本文介绍了在DataGridViewRow中使用时无法删除图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我正在努力解决一个我无法解决的棘手问题。我正在将.bmp图像加载到DataGridView图像列中。多数民众赞成好,我可以看到没有任何问题的图像。但是,稍后在程序中我想删除从中读取图像的文件。但是,无论我尝试什么,我总是在尝试删除文件时遇到异常。



我尝试将单元格设置为null,处理它也是清除网格的Row集合,但在关闭应用程序之前我永远无法访问该文件。



我是否使用错误的方法加载图像?我正在使用Image.FromFile。



我想我以某种方式绑定文件,因为Windows不会让我删除它但我不能绕过它。感谢您的帮助和解释。





该进程无法访问该文件''C:\ Users \Dirk \ Documents \ Visual Studio 2010 \Projects\WindowsFormsApplication9 \ WindsFormsApplication9 \ U + 0001.bmp''因为它正被另一个进程使用





Hi there,

I am struggling with a rather vexing problem that I just can''t solve. I am loading a .bmp image into DataGridView image column. Thats''s ok and I can see the image with no issues. However, later in the program I want to delete the file that the the image was read from. However, no matter what I try, I always get an exception when trying to delete the file.

I have tried setting the cell to null, disposing of it and also clearing the Row collection of the grid but I can never get access to the file until I close the application.

Am I using the wrong method to load the image? I am using Image.FromFile.

I guess that I am somehow tying up the file as Windows won''t let me delete it but I just can''t get around it. Thanks for any help and explanation.


The process cannot access the file ''C:\Users\Dirk\Documents\Visual Studio 2010\Projects\WindowsFormsApplication9\WindowsFormsApplication9\U+0001.bmp'' because it is being used by another process


string Filename = null;

       /// <summary>
       /// Add row and image to the data grid view
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void button5_Click(object sender, EventArgs e)
       {
           dataGridView1.Rows.Add();
           DataGridViewRow xa = dataGridView1.Rows[dataGridView1.Rows.Count - 1];
           string filename = Path.ChangeExtension(Filename, ".bmp");
           xa.Cells["help"].Value = Image.FromFile(filename);
       }

       /// <summary>
       /// Dispose of the datagrid row
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void button4_Click(object sender, EventArgs e)
       {
           dataGridView1.Rows[1].Cells["help"].Value = null;
           dataGridView1.Rows[1].Cells["help"].Dispose();
           dataGridView1.Rows.Clear();
       }

       /// <summary>
       /// Try and delete the file
       /// </summary>
       /// <param name="sender"></param>
       /// <param name="e"></param>
       private void button3_Click(object sender, EventArgs e)
       {
           if (File.Exists(Filename))
           {
               File.Delete(Filename);
           }
       }





谢谢,



Dirk。



Thanks,

Dirk.

推荐答案

这个被另一个进程使用的问题很常见。



由于类似的问题在这里被多次提出,根据这一经验,我知道:在大多数情况下,阻止过程是您自己的过程。您可能忘记在同一个应用程序中处理/关闭某些内容。所以,首先,检查一下。要探索这种可能性,请参阅我过去的答案:

清除C#中的句柄 [ ^ ]。



在这个答案中,注意使用 / code>语句可以帮助您保证在使用后正确处理相应的文件系统对象,而不是保持文件锁定。



在同样的情况下,你真的需要调查哪个进程拥有哪个文件。为此,我建议使用 Sysinternals Suite 中的一个实用程序。这组实用程序(以前来自 Winternals 公司,目前在Microsoft)是必须的对于任何开发人员,请参阅:

http://technet.microsoft.com/en -us / sysinternals / bb842062 [ ^ ],

http://technet.microsoft.com/en-us/sysinternals / bb545027 [ ^ ]。



您需要的实用程序是 handle.exe ,请参阅:

http://technet.microsoft.com/en-us/sysinternals/bb896655 [ ^ ]。



在您的情况下,您使用文件名参数:

This "being used by another process" problem is very usual.

As the similar question was asked here many times, from this experience I know: in most cases the blocking process is your own process. You could have forgotten to dispose/close something in the same application. So, first of all, check it up. To explore this possibility, please see my past answer:
Clearing a Handle in C#[^].

In this answer, pay attention for the using of the using statement which helps you to guarantee that appropriate file system object is properly disposed after use, not keeping the file locked.

In same cases, you really need to investigate which process holds which file. For this, I recommend using one utility from the Sysinternals Suite. This set of utilities (formerly from Winternals company, presently at Microsoft) is a must-have for any developer, please see:
http://technet.microsoft.com/en-us/sysinternals/bb842062[^],
http://technet.microsoft.com/en-us/sysinternals/bb545027[^].

The utility you need is "handle.exe", please see:
http://technet.microsoft.com/en-us/sysinternals/bb896655[^].

In your case, you use it with file name parameter:
handle.exe <file_name>





此实用程序将扫描所有类型的句柄,而不仅仅是文件句柄。对于文件,它将扫描与文件名匹配的所有文件句柄(因此它不必是完整路径名)并返回足以识别每个进程的信息,包括其 pid 。因此,如果您需要有关相关流程的更多信息,您还可以使用其他Sysinternals实用程序,特别是其 Process Explorer

http://technet.microsoft.com/en-us/sysinternals/bb896653 [ ^ ]。



祝你好运,

-SA



This utility will scan all kinds of handles, not just file handles. For file, it will scan all file handles matching the file name (so it does not have to be a full path name) and return information sufficient to identify each process, including its pid. So, if you need more information on a process in question, you can also use other Sysinternals utilities, in particular, its Process Explorer:
http://technet.microsoft.com/en-us/sysinternals/bb896653[^].

Good luck,

—SA


感谢SA,你的文章很好地指出了方向。感谢您的时间。



最终问题在这里描述



http://support.microsoft.com/?id=814675 [ ^ ]



基本上,Image.Load方法会导致文件被锁定在该计划的持续时间。处理它很难。



但我现在使用
Thanks SA, your article pointed the way nicely. Thanks for your time.

Ultimately the problem is described here

http://support.microsoft.com/?id=814675[^]

Basically, the Image.Load method causes the file to be locked for the duration of the program. It''s hard to dispose of it.

However I now load using
// creating a bitmap from a stream
           using (FileStream fileStream = File.Open(Filename, FileMode.Open))
           {
               Bitmap bitmap = new Bitmap(fileStream);
               Image currentPicture = (Image)bitmap;





这不会导致问题。感谢您的帮助,希望这有助于其他人。



and this does not cause the problem. Thanks for the help, hope this helpd others.


这篇关于在DataGridViewRow中使用时无法删除图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆