Windows - 当文件系统中存在 FileNotFound 时,按需下载文件?
我想在 Windows 中放入某种“挂钩”(仅适用于 Windows Server 2008 R2 及更高版本),当我请求磁盘上的文件但该文件不存在时,它会从 Web 服务器请求该文件并将其缓存在本地。
这些文件是不可变的并且具有唯一的文件名。
尝试打开这些文件的应用程序是用 C 编写的,并且只是使用操作系统以正常方式打开文件。假设它调用 OpenFile
请求 c:\scripts\1234.12.script
,然后它就会正常打开它。如果它要求 c:\scripts\1234.13.script
但它不是,那么我在操作系统中的钩子就会向我的网络服务询问该文件,下载它,然后返回该文件文件就如同它一直存在一样。
我更喜欢将其编写为用户模式进程(我从未编写过 Windows 驱动程序),它应该仅在特定文件夹中找不到文件时触发,并且如果可能的话,我更喜欢用托管语言编写它(C# 就完美了)。这些文件很小(< 50kB),并且网络服务速度很快,并且互联网连接令人眼花缭乱,因此我预计下载文件的时间不会超过一秒钟。
我的问题是 - 我从哪里开始寻找有关此类事情的信息?如果有人做过类似的事情 - 你知道我有什么选择(例如可以用 C# 完成吗?)?
I want to put some sort of "hook" into windows (only has to work on Windows Server 2008 R2 and above) which when I ask for a file on disk and it's not there it then requests it from a web server and caches it locally.
The files are immutable and have unique file names.
The application which is trying to open these files is written in C and just opens a file using the operating system in the normal way. Say it calls OpenFile
asking for c:\scripts\1234.12.script
, and that is there then it will just open it normally. If then it asks for c:\scripts\1234.13.script
and it isn't then my hook in the operating system will then go and ask my web service for the file, download it and then return that file as it it were there all the time.
I'd prefer to write this as a usermode process (I've never written a windows driver), it should only fire when files are not found in a specific folder, and I'd prefer if possible to write it in a managed language (C# would be perfect). The files are small (< 50kB) and the web service is fast and the internet connection blinding so I'm not expecting it to take more than a second to download the file.
My question is - where do I start looking for information about this kind of thing? And if anyone has done anything similar - do you know what options I have (eg can it be done in C#?)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要创建一个内核模式文件系统过滤驱动程序,它将拦截打开此类文件的请求并“伪造”这些文件。应该说,即使对于驱动程序开发来说,这也是一个非常复杂的任务。我们的 CallbackFilter 产品将能够解决您的问题,但是“伪造”文件的机制尚未准备好(我们计划为 CallbackFilter 3 提供此功能)。在那之前,我不知道任何用户模式解决方案(坦率地说,也没有内核模式解决方案)可以解决您的问题。
如果您可以更改应用程序正在访问的文件夹,则可以创建虚拟文件系统并将其映射到驱动器号或 NTFS 驱动器上的文件夹。从虚拟文件系统,您可以将大多数请求定向到真实磁盘或从真实磁盘发出,如果文件不存在,您可以下载该文件并缓存它。我们的其他产品回调文件系统可让您在用户模式下执行我所描述的操作。如果您需要完成一项一次性任务,并且没有预算,请无论如何联系我们,也许我们可以找到一些解决方案。还有一个开源解决方案,具有类似(但不是那么全面)的功能,名为 Dokan,但我不会对其质量发表评论。
You would need to create a kernel-mode filesystem filter driver which would intercept requests for opening such files and would "fake" those files. I should say that this is a very complicated task even for driver development. Our CallbackFilter product would be able to solve your problem however mechanism for "faking" files is not yet ready (we plan this feature for CallbackFilter 3). Until then I don't know any user-mode solutions (frankly speaking, no kernel-mode solutions as well) that would solve your problem.
If you can change the folder the application is accessing, then you can create a virtual file system and map it to the drive letter or a folder on NTFS drive. From the virtual file system you can direct most requests to/from real disk and if the file doesn't exist, you can download the file and cache it. Our other product, Callback File System, lets you do what I described in user-mode. If you have a one-time task you need to accomplish, and don't have a budget for it, please contact us anyway and maybe we can find some solution. There also exists an open-source solution with similar (but not so comprehensive) functionality named Dokan, yet I will refrain from commenting on its quality.
您还可以尝试 Dokan ,它是开源的,您可以查看其 讨论组以获取问题和指南。
You can also try Dokan , it open source and you can check its discussion group for question and guides.