在 Windows 服务中使用 SHFileOperation
这是可能的,但是在 Windows 服务中使用 SHFileOperation 是否合适? shell32.dll 中的所有这些 SHxxx API 函数似乎都是根据用户级程序编写的。 我可以确定 SHFileOperation 永远不会显示 GUI 吗?
It's possible, but is it appropriate to use SHFileOperation within a Windows service? All those SHxxx API functions in shell32.dll seem to have been written with user level programs in mind. Can I be certain SHFileOperation won't display GUI ever?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
根据 SHFILEOPTSTRUCT 文档,您可以使用以下标志可防止出现任何 UI:
或(如果您的目标是 Windows Vista)
FOF_NO_UI
,与上面的相同。查看Windows SDK中的
ShellAPI.h
头文件,针对FOF_NO_UI
的注释说“根本不显示任何UI”,因此我认为这是可以的使用SHFileOperation。According to the SHFILEOPTSTRUCT documentation, you can use the following flags to prevent any UI from appearing:
or (if you're targeting Windows Vista),
FOF_NO_UI
, which is the same as the above.Looking in the
ShellAPI.h
header file in the Windows SDK, the comment againstFOF_NO_UI
says "don't display any UI at all", so from this I assume it's OK to useSHFileOperation
.我想说,这并不是不合适或不建议的。 大多数 shell32 API 都是在编写时就基本了解它们将在交互式进程中使用。 我认为没有任何方法可以保证 SHFileOperation 永远不会显示 UI 组件。 事实上,如果您查看 IFileOperation(这是取代SHFileOperation的新Vista接口),它明确指出:
I would say, not it's not appropriate or advisable. Most of the shell32 APIs were written with a basic understanding that they would be used in interactive processes. I don't think there is any way you can guarantee that SHFileOperation will never display a UI component. In fact, if you look at IFileOperation (which is the new Vista interface that replaces SHFileOperation), it clearly states:
我必须同意:不合适或不建议。
使用 SHFileOperation 的主要原因是通过 UI 执行操作,和/或可逆的操作。 即,使用 SHFileOperation 删除文件会将文件放入回收站,而不是删除它们,从而允许当前交互用户取消删除或撤消所执行的操作。
由于服务在非交互式桌面上运行,因此没有人能够清除回收站。
I have to agree: not appropriate or advisable.
The prinicpal reason to use SHFileOperation is to perform operations with a UI, and/or which are reversable. I.e. using SHFileOperation to delete files is going to place the files in a recycle bin rather than deleting them allowing the current interactive user to undelete, or undo the operation performed.
As the services run on a non interactive desktop, no one will ever be able to clear that recycle bin out.
我也遇到了这个问题,并致力于在服务器和网络共享之间实现安全可靠的网络文件复制(大多数共享基于 CIFS / NetApp 文件管理器),并且
SHFileOperation
有时会失败。现在开始使用 ROBOCOPY(默认情况下在 Vista/Server 2008 以上的所有 Microsoft 操作系统中可用),看起来确实很有趣且可靠。
这让我大开眼界:https://stackoverflow.com/a/1030752/559144
I had this issue as well and working at implementing a secure and reliable network file copy between servers and network shares ( most of these shares are CIFS / NetApp filer based ) and
SHFileOperation
fails from time to time.now started using
ROBOCOPY
(available by default in all Microsoft OS from Vista/Server 2008 upwards) and really looks interesting and reliable.this has opened my eyes: https://stackoverflow.com/a/1030752/559144