0

My program opens a file for reading purposes and does not let other programs access its contents. This is exactly what I want to do, except another specific process also needs to read the file. I do not manage this process, so that would be ideal if I could define it by its name or PID.

What I have done:

FileStream fileStream = new FileStream(FILE, FileMode.Open, FileAccess.Read, FileShare.None);

The problem with what I did, as previously stated, was it won't allow the other process to read from it. I know I could just have it close the FileStream, use File. Read, or change FileShare, but I only one my process and the other process to read the file.

  • 1
    To the best of my knowledge there is no generic way for you to achieve that. – 500 - Internal Server Error Aug 27 '21 at 13:48
  • 1
    You can't do what you ask. You can't share a file "for some other process". You either allow file sharing or not. You can use permissions to restrict the *accounts* that can access that file – Panagiotis Kanavos Aug 27 '21 at 13:48
  • Does this answer your question? [How to limit folder/file access to a program only?](https://stackoverflow.com/questions/20010372/how-to-limit-folder-file-access-to-a-program-only) and [Limiting access to a file based on process group](https://stackoverflow.com/questions/13312148/limiting-access-to-a-file-based-on-process-group) –  Aug 27 '21 at 16:19
  • In addition to the suggested duplicates, one way could be to set the file permissions for a specific Windows account and start authorized processes as logged using that account (I don't know how to say this in English and I never tried that - it is for example like SQL Server when it [starts with another account](https://stackoverflow.com/questions/4624113/start-a-net-process-as-a-different-user) than the current user, or like SYSTEM processes themselves). –  Aug 27 '21 at 16:21

0 Answers0