C# 关闭文件时删除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/546049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 07:39:42  来源:igfitidea点击:

Delete on close files

c#filefilesystemstemporary-files

提问by xoreax

Language used: C#

使用语言:C#

Theory: I want to create a file with the flag FileOptions.DeleteOnClosein a temporary folder. The file is successfully created and I write dato onto it, the next step is to launch the application associated with the file Process.Start(...)and allow the user to inspect the document, finally I close my handle and as soon as other process close the handle to the temporary file, the file is deleted by operating system.

理论:我想FileOptions.DeleteOnClose在临时文件夹中创建一个带有标志的文件。文件已成功创建,我将 dato 写入其中,下一步是启动与文件关联的应用程序Process.Start(...)并允许用户检查文档,最后我关闭我的句柄,并在其他进程关闭临时句柄时文件,该文件被操作系统删除。

My problem is that other processes cannot open the file, even for reading, despite if I add FileShare.ReadWrite | FileShare.Deleteto the sharing mode.

我的问题是,即使我添加FileShare.ReadWrite | FileShare.Delete到共享模式,其他进程也无法打开文件,即使是阅读也是如此。

Any suggestions?

有什么建议?

采纳答案by Duncan Smart

The other processes need to specify FileShare.Deletewhen they open the DeleteOnClosefile

其他进程需要指定FileShare.Delete何时打开DeleteOnClose文件

From the MSDN CreateFiledocs:

来自MSDN CreateFile文档:

"FILE_FLAG_DELETE_ON_CLOSE... Subsequent open requests for the file fail, unless the FILE_SHARE_DELETEshare mode is specified."

FILE_FLAG_DELETE_ON_CLOSE... 对文件的后续打开请求失败,除非FILE_SHARE_DELETE指定了共享模式。”

回答by Bodo Thiesen

Switch to Linux scnr

切换到 Linux scnr

Ok, seriously now: That is a flaw in the Windows operating system which can't really be worked around. Each program opening the file must agree on other programs having the file open in the same time. That was a problem I got many years back when I still used Windows as well. It doesn't suffice to open a file and say: Let anyone else open this as well. The others must also say open this file even if it's open already.

好的,现在认真:这是 Windows 操作系统中无法真正解决的缺陷。每个打开文件的程序必须与同时打开文件的其他程序一致。这是我多年前还在使用 Windows 时遇到的问题。打开文件并说:让其他人也打开它是不够的。其他人也必须说打开这个文件,即使它已经打开。

On Linux on the contrary, the operating system doesn't allow any file locking in the way Windows does at all. Here, if any file is used by more than one program simultaneously, the programs itself must make sure, that concurrent accesses get locked out. Additionally, on Linux, we can just create the file, make sure the other process has been started and opened the file and then just delete the file (while it is open). The filename is then removed from the file system immediatelly, but the file is still maintained by the file system driver until the last link (including open file handles) got removed.

相反,在 Linux 上,操作系统根本不允许像 Windows 那样锁定任何文件。在这里,如果任何文件被多个程序同时使用,程序本身必须确保并发访问被锁定。此外,在 Linux 上,我们可以只创建文件,确保其他进程已启动并打开文件,然后删除文件(当它打开时)。然后立即从文件系统中删除文件名,但该文件仍由文件系统驱动程序维护,直到最后一个链接(包括打开的文件句柄)被删除。

Back to your problem: As all of this doen't work on Windows, you could do two other approaches:

回到你的问题:由于所有这些在 Windows 上都不起作用,你可以做另外两种方法:

  1. Register the file to be deleted on next boot (in the Win3x days, there was a section in the win.ini for that. Newer Windows version still support that, I just can't recall any longer, how it's done now).
  2. Start the other process, wait for it to open the file, close the file and then try each minute to delete the file until deletion succeeds ...
  1. 注册要在下次启动时删除的文件(在 Win3x 时代,win.ini 中有一个部分用于该部分。较新的 Windows 版本仍然支持该部分,我只是想不起来了,现在是如何完成的)。
  2. 启动另一个进程,等待它打开文件,关闭文件,然后每分钟尝试删除文件,直到删除成功...

Regards, Bodo

问候, 博多

回答by paxdiablo

Check this:

检查这个:

You need to make sure that allprocesses are opening the file with FileShare.ReadWrite and FileShare.Delete.

您需要确保所有进程都使用 FileShare.ReadWrite 和 FileShare.Delete 打开文件。

Even if the creator opens with share-readwrite, if a second program tries to open with share-read, the second program is basically saying no-one else can write. But the first program already has that power so the second open fails.

即使创建者以共享读取打开,如果第二个程序尝试以共享读取打开,第二个程序基本上是在说没有其他人可以写入。但是第一个程序已经具有这种功能,因此第二个打开失败。