我们能否在 Windows 中删除打开的文件? [英] Will we ever be able to delete an open file in Windows?

查看:24
本文介绍了我们能否在 Windows 中删除打开的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我主要是一名 linux/web 开发人员.

Disclaimer: I am mainly a linux/web developer.

Windows 有这个不错"的功能,它拒绝删除任何进程保持打开的任何文件的权限.因此,如果防病毒软件在错误的时间击中错误的文件,某些随机程序可能会出现异常行为并可能崩溃.

Windows has this "nice" feature where it denies permission to delete any file that is held open by any process. So if an antivirus hits the wrong file at the wrong time, some random program might misbehave and possibly crash.

我说得对吗?有没有计划解决这个问题?

Am I right? Are there plans to fix this?

你们中有人认为这是可以接受的吗,或者在当时怎么可能是个好主意?

Do any of you find this acceptable, or, how could it possibly seem a good idea at the time?

它在 Unix 上的工作方式非常不同,几十年来一直如此.

It works very differently on Unix, and has been so for decades.

举个例子:

  • 进程 1 打开 foo.txt,无论是读还是写,或者两者兼而有之,都没有关系
  • 进程 2 删除文件
  • 该文件已与文件系统取消链接
  • 进程 1 继续读取和/或写入,文件仍然存在,只要磁盘上有空间,它就可以增长.它只是无法从还没有文件句柄的其他进程访问.
  • 当进程 1 关闭文件时,它将无法从任何地方访问

实际上,Unix 上常见的临时文件使用模式是:open-remove-read/write-close.

Actually, a common usage pattern for temporary files on Unix is: open-remove-read/write-close.

推荐答案

您的初始陈述不正确.Windows 允许删除打开的文件.您只需要指定 FILE_SHARE_DELETE 就可以了.细心的程序员应该明智地决定该标志(或共享以进行读/写)是否有意义并通过它.

Your initial statement is not correct. Windows does allow open files to be deleted. You just have to specify FILE_SHARE_DELETE and you're all set. Careful programmers should sensibly decide if that flag (or sharing for reading/writing) make sense and pass it.

在启用完全共享(包括删除)的情况下无法打开文件的防病毒产品有问题.

An anti virus product that does not open files with full sharing (including deletion) enabled is buggy.

然而,Windows 会记住任何进程的当前工作目录并防止它被删除.该工作目录独立于进程打开的任何文件的位置.

Windows does, however, remember the current working directory of any process and prevents it from being deleted. This working directory is independent of the location of any files opened by the process.

这篇关于我们能否在 Windows 中删除打开的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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