简体   繁体   English

文件正在由C#中的另一个进程使用

[英]File is being used by another process in c#

I am trying to delete a file in C#, however I am receiving a message that the file is used from another process. 我正在尝试删除C#中的文件,但是我收到一条消息,指出该文件已被另一个进程使用。 What I want to do is to check if the files exists and close it. 我要做的是检查文件是否存在并关闭它。 I am using the following function in order to check if the file is open: 我正在使用以下功能来检查文件是否打开:

public static bool IsFileInUse(string path)
    {
        if (string.IsNullOrEmpty(path))
            throw new ArgumentException("'path' cannot be null or empty.", "path");

        try
        {
            using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read)) { }
        }
        catch (IOException)
        {
            return true;
        }

        return false;
}

and I am trying when the file is in use to close it: 我正在尝试使用文件关闭它时:

bool checking = IsFileInUse(file );
File.Create(file ).Close();
if (File.Exists(file))
{
       File.Delete(file );
}

I got issues in File.Create line, I am receiving the message: 我在File.Create行中遇到问题,我收到消息:

File is being used by another process. 文件正在由另一个进程使用。

EDIT: I am trying to use lock approach in order to delete the file. 编辑:我正在尝试使用锁定方法以删除文件。 Am I suppose to delete the file inside a lock statement? 我是否应该删除lock语句中的文件? How Can I use properly the lock statement? 如何正确使用lock语句?

Why do you suppose that a reading operation will fail if file is in use while a writing operation will not? 您为什么假设如果正在使用文件而没有使用写入操作,则读取操作将失败? File.Create() will fail exactly as new FileStream() failed before... File.Create()将完全像新FileStream()之前失败一样失败。

See also IOException: The process cannot access the file 'file path' because it is being used by another process . 另请参见IOException:该进程无法访问文件“文件路径”,因为它正在被另一个进程使用

Note that your check will fail if the other process didn't open that file exclusively (check FileShare enumeration): file may be open for shared reading, writing and sometimes even for deleting (for example you may be able to read concurrently but not writing however the other process may let you delete that file...). 请注意,如果其他进程未专门打开该文件,则检查将失败(请检查FileShare枚举):该文件可能已打开以进行共享的读取,写入,有时甚至是删除(例如,您可以同时读取但不能写入)但是其他过程可能会让您删除该文件...)。

To close an open file can be really disruptive for the other process, it may crash, nicely handle the problem or...anything else (silently ignore that error and produce random output, open file again and so on...) Is it possible to do it in C#? 关闭打开的文件可能会对其他过程造成破坏,可能会崩溃,很好地解决问题或其他任何问题(静默忽略该错误并产生随机输出,再次打开文件等)。可以用C#做​​到吗? Yes with some P/Invoke... 是的,有一些P / Invoke ...

1) Let's find the handle for the file you want to unlock . 1)让我们找到您想要解锁的文件的句柄。 Use NtQuerySystemInformation() and enumerate all handles until you find the one that refers to that file. 使用NtQuerySystemInformation()并枚举所有句柄,直到找到引用该文件的句柄为止。

2) Duplicate that handle to be valid in your own process using DuplicateHandle() . 2)使用DuplicateHandle()复制该句柄在您自己的进程中有效。

3) Close just create handle specifying DUPLICATE_CLOSE_SOURCE , it will close both your handle and the original one (of course if your process has enough permissions). 3)关闭只是创建指定DUPLICATE_CLOSE_SOURCE句柄,它将同时关闭您的句柄和原始句柄(当然,如果您的进程具有足够的权限)。

4) Check if file is really closed calling NtQuerySystemInformation() again, if not then you may need to directly close its parent process. 4)检查文件是否真的关闭,再次调用NtQuerySystemInformation() ,否则,您可能需要直接关闭其父进程。

You have no need to check if the file exists, just try do delete it: 您无需检查文件是否存在,只需尝试删除

https://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx

If the file to be deleted does not exist, no exception is thrown. 如果要删除的文件不存在,则不会引发异常。

Try and check the exception 尝试检查异常

  try {
    File.Delete(file);
  }
  catch (IOException) {
    // File in use and can't be deleted; no permission etc.
  }

In your code, you don't do anything with the IsFileInUse result. 在代码中,对IsFileInUse结果不执行任何操作。

This File.Create(file ).Close(); 这个File.Create(file ).Close(); will also not close a file that is opened by another process. 也不会关闭由另一个进程打开的文件。 You need to close the process that has the file open, and if it is your own app, close the file handle before trying to delete the file. 您需要关闭打开文件的进程,如果它是您自己的应用程序,请在尝试删除文件之前关闭文件句柄。

bool checking = IsFileInUse(file );
File.Create(file ).Close();
if (!checking) 
{
     if (File.Exists(file))
     {
            File.Delete(file );
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM