允许用户删除文件正在使用通过我的应用程序? [英] Allow User to Delete File In Use By My Application?

查看:129
本文介绍了允许用户删除文件正在使用通过我的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以允许用户删除一个文件,在Windows下,也就是在使用我的应用程序?



在我的情况我有一个快速添加目录由我的应用程序监视。当它看到一个新的形象展示了它会创建一个新的幻灯片以自动幻灯片显示。我还要让我的用户删除(和/或重命名)从我的快速添加目录中的文件,并从幻灯片中删除它。



时有办法,我可以标记,通知窗户我没事了它,而我的应用程序正在使用它删除文件的文件?


解决方案

有没有一种方法,我可以标记,通知窗户我没事了它,而我的应用程序正在使用它?


删除文件的文件< /块引用>

是的。在Win32这是传递给的 dwShareMode 0.85)的.aspx相对=nofollow> 的CreateFile() 。这是您想使另一个进程做一个比特。您正在寻找该位 FILE_SHARE_DELETE ,允许删除或同时打开重命名。然而,对于最有礼貌的行为,我会建议包括所有3, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE [1]



既然你问有关C#,而不是Win32中,在.NET世界这些都包含在 文件共享 枚举,你可以打开一个文件时,如通过。在 File.Open



请注意,这些标志不会让您允许文件的父目录的重命名,而它是由名义开立。不管共享,这将失败归因于内核中的(技术上的Ntfs.sys IIRC)的限制。






脚注



1:编辑点评:这是一个耻辱,通过 0 (其中很多人都不假思索地)恰好是最宽松的选项,或者更多的人在Windows上编写代码并没有意识到这个参数做什么,并通过这三个标志。


Can I allow a user to delete a file, under Windows, that is in use by my application?

In my scenario I have a "quick add" directory that is monitored by my application. When it sees a new image show up it creates a new slide to display in an automated slide show. I would also like to allow my users to delete (and/or rename) a file from my "quick add" directory, and remove it from the slide show.

Is there a way I can flag the file that notifies Windows that I'm okay for it to remove the file while my application is using it?

解决方案

Is there a way I can flag the file that notifies Windows that I'm okay for it to remove the file while my application is using it?

Yes. In Win32 this is dwShareMode passed to CreateFile(). This is a bitfield of what you would like to permit another process to do. The bit you are looking for is FILE_SHARE_DELETE which allows a delete or a rename while open. However, for the most polite behavior I would recommend including all 3, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE.[1]

Since you're asking about C# and not Win32, in the .NET world these are included in the FileShare enumeration which you can pass when opening a file, eg. in File.Open.

Note that these flags don't let you allow a rename of a file's parent directory while it is opened by name. That will fail regardless of sharing due to limitations in the kernel (technically in ntfs.sys IIRC).


Footnote

1: Editorial comment: It is a shame that passing 0 (which many people do without thinking) happens to be the least permissive option, or that more people writing code on Windows don't realize what this parameter does and pass these three flags.

这篇关于允许用户删除文件正在使用通过我的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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