如何关闭正在使用的文件 [英] How to close a file in use

查看:463
本文介绍了如何关闭正在使用的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建pdf并通过电子邮件发送pdf的项目,

首先,我删除所有pdf,然后创建所需的pdf

我将所有文件放在某个路径中,检查文件名是否是要删除的pdf之一,然后将其删除

i have a project that create a pdf and send it via email,

at first, i delete all the pdf''s and then create the needed one

i get all the file in a cetain path, check if the file name is one of the pdf wanted to be deleted, and then delete it

string[] filePaths = Directory.GetFiles(Application.StartupPath);
foreach (string filename in filePaths)
{
    if (filename.Contains("FILE NAME") )
    {
          try
          {
              File.Delete(filename);
          }
          catch (SystemException ex)
          {
              exception.SendExceptionEMail(ex.ToString());
          }
     }
}




有时会生成异常

该进程无法访问文件文件路径",因为它正在被另一个进程使用.

我想知道如果此异常产生后可以删除该文件,该如何关闭该文件




it some times generates an exception

The process cannot access the file "File Path" because it is being used by another process.

i want to know how to close the file if this exception produced to be able to delete it

推荐答案

您不能,除非您终止使该文件保持打开状态的过程.

首先,这里多次问过类似的问题,根据这种经验,我知道:在大多数情况下,阻止过程是您自己的过程.您可能忘记了在同一应用程序中处理/关闭某些内容.因此,首先,进行检查.要探索这种可能性,请查看我过去的回答:
在C#中清除句柄 [ 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 [ ^ ].

在您的情况下,可以将其与文件名参数一起使用:
You cannot, unless you terminate the process which keeps this file open.

First of all, the similar question was asked here many times, and 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 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


这篇关于如何关闭正在使用的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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