打开前检查文件是否已被使用(网络驱动器,C#)

时间:2023-01-26 22:11:00

Does there exist any way in .Net to check before opening if a file (on a local network drive) is already in use?

如果文件(在本地网络驱动器上)已经在使用中,在打开之前是否有任何方法可以检查.Net?

6 个解决方案

#1


3  

You should try to access it and if it failed, you either don't have required permissions (which you can check with GetAccessControl) or it's locked by another process.

您应该尝试访问它,如果失败,您要么没有所需的权限(可以使用GetAccessControl检查),要么被其他进程锁定。

But I don't think there is any reliable measure to distinguish a lock and a permission failure (since you might be able to read the file, but not able to check the permissions). Even Windows' error message says you either don't have permission or it's being used by another process.

但我认为没有任何可靠的措施来区分锁和权限失败(因为您可能能够读取文件,但无法检查权限)。甚至Windows的错误消息表明您要么没有权限,要么被另一个进程使用。

You can use WMI CIM_DataFile class to query InUseCount for a specified data file.

您可以使用WMI CIM_DataFile类来查询指定数据文件的InUseCount。

If you're looking for a programmatical equivalent to lsof utility in Linux, to find out all open files by a given local process, you could try using Win32_Process WMI class through System.Management namespace. You could issue a WMI query to look up the file name in all open files being used by all local processes too see if it's there or not. Alternatively, you could P/Invoke and use NtQuerySystemInformation API directly to accomplish the same task.

如果您正在寻找与Linux中的lsof实用程序等效的程序,要查找给定本地进程的所有打开文件,您可以尝试通过System.Management命名空间使用Win32_Process WMI类。您可以发出WMI查询以查找所有本地进程正在使用的所有打开文件中的文件名,也可以查看它是否存在。或者,您可以直接P / Invoke并使用NtQuerySystemInformation API来完成相同的任务。

#2


2  

This will do. FileShare.None as mentioned in MSDN :

这样做。 MSDN中提到的F​​ileShare.None:

None : Declines sharing of the current file. Any request to open the file (by this process or another process) will fail until the file is closed.

无:拒绝共享当前文件。在文件关闭之前,任何打开文件的请求(通过此进程或其他进程)都将失败。

File.Open(name, FileMode.Open, FileAccess.Read, FileShare.None);

EDIT : Remember to wrap in a Try/Catch block, and using FileShare.None actually means you want to open the file exclusively.

编辑:记住包装在Try / Catch块中,并使用FileShare.None实际上意味着您要独占打开文件。

#3


2  

bool CanReadAndWrite(string path)
{
    var perm = new System.Security.Permissions.FileIOPermission(
         System.Security.Permissions.FileIOPermissionAccess.Write |
         System.Security.Permissions.FileIOPermissionAccess.Read,
         path);
    try
    {
         perm.Demand();
         return true;
    }
    catch 
    {
         return false;
    }
}

Checks to see if you can read and write to a file.

检查您是否可以读取和写入文件。

#4


1  

Use System.IO.File.OpenWrite(path). Surround it in a try/catch block, and if it is already open for writing somewhere else, you will get a System.UnauthorizedAccessException.

使用System.IO.File.OpenWrite(path)。将它包含在try / catch块中,如果它已经打开以便在其他地方写入,则会得到System.UnauthorizedAccessException。

#5


0  

The following syntax will help:

以下语法将有所帮助:

FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

FileStream s2 = new FileStream(name,FileMode.Open,FileAccess.Read,FileShare.Read);

#6


0  

In general, you should always avoid relying on file accessibility checks. The reason is that it might be, and then it may change just a couple of cycles before you access it.

通常,您应始终避免依赖文件可访问性检查。原因可能是,它可能会在您访问它之前改变几个周期。

Instead, just try to open it and see if you have any errors.

相反,只是尝试打开它,看看你是否有任何错误。

#1


3  

You should try to access it and if it failed, you either don't have required permissions (which you can check with GetAccessControl) or it's locked by another process.

您应该尝试访问它,如果失败,您要么没有所需的权限(可以使用GetAccessControl检查),要么被其他进程锁定。

But I don't think there is any reliable measure to distinguish a lock and a permission failure (since you might be able to read the file, but not able to check the permissions). Even Windows' error message says you either don't have permission or it's being used by another process.

但我认为没有任何可靠的措施来区分锁和权限失败(因为您可能能够读取文件,但无法检查权限)。甚至Windows的错误消息表明您要么没有权限,要么被另一个进程使用。

You can use WMI CIM_DataFile class to query InUseCount for a specified data file.

您可以使用WMI CIM_DataFile类来查询指定数据文件的InUseCount。

If you're looking for a programmatical equivalent to lsof utility in Linux, to find out all open files by a given local process, you could try using Win32_Process WMI class through System.Management namespace. You could issue a WMI query to look up the file name in all open files being used by all local processes too see if it's there or not. Alternatively, you could P/Invoke and use NtQuerySystemInformation API directly to accomplish the same task.

如果您正在寻找与Linux中的lsof实用程序等效的程序,要查找给定本地进程的所有打开文件,您可以尝试通过System.Management命名空间使用Win32_Process WMI类。您可以发出WMI查询以查找所有本地进程正在使用的所有打开文件中的文件名,也可以查看它是否存在。或者,您可以直接P / Invoke并使用NtQuerySystemInformation API来完成相同的任务。

#2


2  

This will do. FileShare.None as mentioned in MSDN :

这样做。 MSDN中提到的F​​ileShare.None:

None : Declines sharing of the current file. Any request to open the file (by this process or another process) will fail until the file is closed.

无:拒绝共享当前文件。在文件关闭之前,任何打开文件的请求(通过此进程或其他进程)都将失败。

File.Open(name, FileMode.Open, FileAccess.Read, FileShare.None);

EDIT : Remember to wrap in a Try/Catch block, and using FileShare.None actually means you want to open the file exclusively.

编辑:记住包装在Try / Catch块中,并使用FileShare.None实际上意味着您要独占打开文件。

#3


2  

bool CanReadAndWrite(string path)
{
    var perm = new System.Security.Permissions.FileIOPermission(
         System.Security.Permissions.FileIOPermissionAccess.Write |
         System.Security.Permissions.FileIOPermissionAccess.Read,
         path);
    try
    {
         perm.Demand();
         return true;
    }
    catch 
    {
         return false;
    }
}

Checks to see if you can read and write to a file.

检查您是否可以读取和写入文件。

#4


1  

Use System.IO.File.OpenWrite(path). Surround it in a try/catch block, and if it is already open for writing somewhere else, you will get a System.UnauthorizedAccessException.

使用System.IO.File.OpenWrite(path)。将它包含在try / catch块中,如果它已经打开以便在其他地方写入,则会得到System.UnauthorizedAccessException。

#5


0  

The following syntax will help:

以下语法将有所帮助:

FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

FileStream s2 = new FileStream(name,FileMode.Open,FileAccess.Read,FileShare.Read);

#6


0  

In general, you should always avoid relying on file accessibility checks. The reason is that it might be, and then it may change just a couple of cycles before you access it.

通常,您应始终避免依赖文件可访问性检查。原因可能是,它可能会在您访问它之前改变几个周期。

Instead, just try to open it and see if you have any errors.

相反,只是尝试打开它,看看你是否有任何错误。