等待在C#中移动文件之前获得文件的独占访问权限

时间:2021-08-07 20:53:41

I have a requirement to move certain files after they has been processed. Another process access the file and I am not sure when it releases them. Is there any way I can find out when the handle to the file has been released so I can move them at that time.

我需要在处理完文件后移动它们。另一个进程访问该文件,我不确定它何时释放它们。有什么方法可以找到文件的句柄何时被释放,所以我可以在那时移动它们。

I am using Microsoft C# and .Net framework 3.5.

我正在使用Microsoft C#和.Net framework 3.5。

Cheers, Hamid

3 个解决方案

#1


If you have control of both the producer of the file and the consumer, the old trick to use is create the file under a different name, and rename it once complete. For example, say the producer is creating files always called file_.txt, and your consumer is scanning for all files beginning file_, then the producer can do this: 1. Create the file called tmpfile_.txt 2. When the file is written, the producer simply renames the file to file_.txt

如果您同时控制文件的生产者和使用者,则使用的旧技巧是以不同的名称创建文件,并在完成后重命名。例如,假设生产者正在创建始终称为file_.txt的文件,并且您的消费者正在扫描所有以file_开头的文件,那么生产者可以这样做:1。创建名为tmpfile_.txt的文件2.文件写入时,生产者只需将文件重命名为file_.txt

The rename operation is atomic, so once your consumer sees its available, it is safe to open it.

重命名操作是原子操作,因此一旦您的消费者看到它可用,就可以安全地打开它。

Of course, this answer depends on if you are writing both programs.

当然,这个答案取决于你是否正在编写这两个程序。

HTH Dermot.

#2


Just contniually try to open the file for exclusive writing? (e.g. pass FileShare.None to the FileStream constructor). Once you have opened it, you know no one else is using it. However, this might not be the best way to do what you're doing.

只是继续尝试打开文件进行独家写作? (例如,将FileShare.None传递给FileStream构造函数)。一旦你打开它,你知道没有其他人在使用它。但是,这可能不是您正在做的事情的最佳方式。

If you're after two way communication, see if the other program can be talked to via a pipe.

如果您正在进行双向通信,请查看是否可以通过管道与其他程序通信。

#3


If you have control of both of the sources, use a named mutex (which works across processes) to control access to the files rather than locking the file at the filesystem level. This way, you don't have to catch the exception raised by attempting to lock a locked file and loop on that, which is rather inelegant.

如果您可以控制这两个源,请使用命名的互斥锁(它跨进程工作)来控制对文件的访问,而不是在文件系统级别锁定文件。这样,您不必通过尝试锁定锁定文件并在其上循环来捕获引发的异常,这是相当不优雅的。

#1


If you have control of both the producer of the file and the consumer, the old trick to use is create the file under a different name, and rename it once complete. For example, say the producer is creating files always called file_.txt, and your consumer is scanning for all files beginning file_, then the producer can do this: 1. Create the file called tmpfile_.txt 2. When the file is written, the producer simply renames the file to file_.txt

如果您同时控制文件的生产者和使用者,则使用的旧技巧是以不同的名称创建文件,并在完成后重命名。例如,假设生产者正在创建始终称为file_.txt的文件,并且您的消费者正在扫描所有以file_开头的文件,那么生产者可以这样做:1。创建名为tmpfile_.txt的文件2.文件写入时,生产者只需将文件重命名为file_.txt

The rename operation is atomic, so once your consumer sees its available, it is safe to open it.

重命名操作是原子操作,因此一旦您的消费者看到它可用,就可以安全地打开它。

Of course, this answer depends on if you are writing both programs.

当然,这个答案取决于你是否正在编写这两个程序。

HTH Dermot.

#2


Just contniually try to open the file for exclusive writing? (e.g. pass FileShare.None to the FileStream constructor). Once you have opened it, you know no one else is using it. However, this might not be the best way to do what you're doing.

只是继续尝试打开文件进行独家写作? (例如,将FileShare.None传递给FileStream构造函数)。一旦你打开它,你知道没有其他人在使用它。但是,这可能不是您正在做的事情的最佳方式。

If you're after two way communication, see if the other program can be talked to via a pipe.

如果您正在进行双向通信,请查看是否可以通过管道与其他程序通信。

#3


If you have control of both of the sources, use a named mutex (which works across processes) to control access to the files rather than locking the file at the filesystem level. This way, you don't have to catch the exception raised by attempting to lock a locked file and loop on that, which is rather inelegant.

如果您可以控制这两个源,请使用命名的互斥锁(它跨进程工作)来控制对文件的访问,而不是在文件系统级别锁定文件。这样,您不必通过尝试锁定锁定文件并在其上循环来捕获引发的异常,这是相当不优雅的。