开发者

Is it possible to substitue a MemoryStream or StreamReader when an object expects a filename? (C#/.NET)

开发者 https://www.devze.com 2023-04-05 03:31 出处:网络
Say you have a method 开发者_StackOverflowor property on a third party sealed component that expects a file name. You do not have the source for the third party component. You want that component to d

Say you have a method 开发者_StackOverflowor property on a third party sealed component that expects a file name. You do not have the source for the third party component. You want that component to do what it's supposed to do (read only) on a file you have, but your file is encrypted on disk and you only want the decrypted version in memory so it cannot be easily copied in its plain form.

Is it possible to create a wrapper or some other approach to trick the component to think it's reading from a file when it's actually reading from a MemoryStream? Or is it totally impossible? Can it be done outside .NET in native Windows code?

Thanks


You can't do that the way that you are proposing, no. My recommendation would be to use the Encrypting Filesystem functionality built into windows. That way the file is stored in encrypted form on disk, but is available via the normal IO methods to the application (provided that the account that is running the application has access to the file).


Can it read from "CON" as input (like many text utilities grep/findstr, more,...)? In this case you can try to redirect input/output stream and feed results thata way.


Is it possible to create a wrapper or some other approach to trick the component to think it's reading from a file when it's actually reading from a MemoryStream?

No, sorry. You will have to decrypt the file into a temporary file and then pass this temporary file to the plugin. Once it finishes its work delete the temporary file.


This short answer is if a component is expecting a filename e.g. a string you can not parse it a memory stream.

However if the file is encrypted with Encrypting File System (EFS) or something native to Windows it may be able to decrypt the file without knowing the file is encrypted.

These might help:

http://en.wikipedia.org/wiki/Encrypting_File_System

http://en.wikipedia.org/wiki/BitLocker_Drive_Encryption


You could have a look at Dokan. I haven't tried it, but it's a way of creating a virtual file system in .Net.


You can create an in-memory disk drive (either in code or by using third-party application) and put a file there. Another approach is to implement virtual file system and handle all file requests. Both approaches are possible for example using our Virtual Storage products.

Also, I don't know about .NET in particular, but in Windows you can hook API functions and substitute certain operations (including file operations). There even exist components for this, but, again, I don't know if they offer their functionality for .NET.

0

精彩评论

暂无评论...
验证码 换一张
取 消